Talk:Sort stability: Difference between revisions

Content added Content deleted
(reply re J stability)
Line 7: Line 7:
==J and stability?==
==J and stability?==
Please excuse me, I don't intend to bang on about J, but sorting in ascending order might not be the same thing as a stable sort. The example given in the task description I would say does ''not'' sort other columns in ascending order. If the column being sorted on (the second column), has the same values then the relative order of those rows must stay the same. The table has the US and UK Birmingham rows in ''reverse'' alphabetical order w.r.t. the first column. A stable sort would leave the US above the UK Birmingham. If J would swap them then it does not do a stable sort. --[[User:Paddy3118|Paddy3118]] 20:25, 28 August 2009 (UTC)
Please excuse me, I don't intend to bang on about J, but sorting in ascending order might not be the same thing as a stable sort. The example given in the task description I would say does ''not'' sort other columns in ascending order. If the column being sorted on (the second column), has the same values then the relative order of those rows must stay the same. The table has the US and UK Birmingham rows in ''reverse'' alphabetical order w.r.t. the first column. A stable sort would leave the US above the UK Birmingham. If J would swap them then it does not do a stable sort. --[[User:Paddy3118|Paddy3118]] 20:25, 28 August 2009 (UTC)
:Hi Paddy, No problem. It doesn't talk about <i>sorting</i> in ascending order. It says "Elements of /:y that select equal elements of y are in ascending order" - essentially that equal elements will appear in order of appearance.
<lang j> /: 'adfafb'
0 3 5 1 2 4</lang>
This is the grade and tells us that to sort the list in ascending order we'd arrange the list by its indices as shown. In this case the equal elements of y are the two a's and the two f's. The a's have indices 0 & 3, the f's have indices 2 & 4. The elements of /:y that select equal elements of y appear in ascending order so the 0 appears before the 3 in the grade and the 2 appears before the 4. Hope that helps!
<lang j> NB. sort txt by txt without the first 4 columns
txt /: 4&}."1 txt
US Birmingham
UK Birmingham
UK London
US New York
</lang>






The indices of <tt>a</tt> in the list are <tt>0</tt> and <tt>3</tt>.
<tt>/:y</tt> doesn't sort <tt>y</tt> it produces a integer vector of each elements because the elements of /:y (the grade) <i>equal</i> elements of columns being used to determine the sort order are sorted are "allocated" a