Sort stability

Sort stability
You are encouraged to solve this task according to the task description, using any language you may know.

When sorting records in a table by a particular column or field, a stable sort will always retain the relative order of records that have the same key.

For example, in this table of countries and cities, a stable sort on the second column, the cities, would keep the US Birmingham above the UK Birmingham. (Although an unstable sort might, in this case, place the US Birmingham above the UK Birmingham, a stable sort routine would guarantee it).

UK London
US New York
US Birmingham
UK Birmingham

Similarly, stable sorting on just the first column would generate “UK London” as the first item and “US Birmingham” as the last item (since the order of the elements having the same first word – “UK” or “US” – would be maintained).

Examine the documentation on any in-built sort routines supplied by a language.

There is no built-in function in C language. stdlib which comes with any C implementation is required to provide a qsort() routine that can sort arbitrary datatypes. Although the sorting algorithm is not specified, most (all?) implementions use a combined quicksort/insertion sort method for efficiency. Quicksort is by nature unstable.

Before Sort by city
UK London
US New York
US Birmingham
UK Birmingham
After Sort by city
US Birmingham
UK Birmingham
UK London
US New York
Before Sort by country
UK London
US New York
US Birmingham
UK Birmingham
After Sort by country
UK London
UK Birmingham
US New York
US Birmingham

Note(1): The built-in sort handles lists of mixed types by sorting first by type and then value. No coercion of types is performed. The sort order of types is: &null, integer, real, string, cset, procedure, list, set, table, record.

Before sort:
UK London
US New York
US Birmingham
UK Birmingham
After sort on city:
US Birmingham
UK Birmingham
UK London
US New York
Before sort:
UK London
US New York
US Birmingham
UK Birmingham
After sort on country:
UK London
UK Birmingham
US New York
US Birmingham

//An array of pairs, order by the right hand element of the pair: with i in array order by #i->second do => { … }

//The array can also be ordered by multiple values:with i in array order by #i->second, #i->first do => { … }

Sorting of arrays by either method uses “Qucksort” and is therefore unstable. A simulation of increasing sort stability would be introduced with additional params such as the example of ordering by the second then the first pair values in the example above - but would not be guaranteed stable.

Note this explanation of sorting does not cover ordering by properties of complex objects, which is also possible using query expressions.

Sort is not always stable. Ordering, which gives a list of indices such as to put the elements of the list in order, is stable. An example would be to sort the list (of lists) {{1, 2, 3}, {4, 5, 6}, {5, 4, 3}, {9, 5, 1}}, and doing so by looking at the 2nd value of each list:

MathWorks' policy seems to be that their built-in sorting algorithm will always be a stable sort across all versions (reference). To check to see if your version of MATLAB provides a stable sort,check the output of command "help sort".

Before sort:
UK London
US New York
US Birmingham
UK Birmingham
After sort on city:
US Birmingham
UK Birmingham
UK London
US New York
Before sort:
UK London
US New York
US Birmingham
UK Birmingham
After sort on country:
UK London
UK Birmingham
US New York
US Birmingham

Original table
--------------------------------------------------------------------------------
UK London
US New York
US Birmingham
UK Birmingham
Unstable sort on city
--------------------------------------------------------------------------------
UK Birmingham
US Birmingham
UK London
US New York
Stable sort on city
--------------------------------------------------------------------------------
US Birmingham
UK Birmingham
UK London
US New York
Unstable sort on country
--------------------------------------------------------------------------------
UK London
UK Birmingham
US Birmingham
US New York
Stable sort on country
--------------------------------------------------------------------------------
UK London
UK Birmingham
US New York
US Birmingham

---------------------------
Message
---------------------------
by country
UK London
UK Birmingham
US New York
US Birmingham
by city
US Birmingham
UK Birmingham
UK London
US New York
---------------------------
OK
---------------------------

PureBasic's includes two built-in sort functions for arrays, SortArray() and SortStructuredArray(), and two built-in sort functions for linked lists, SortList() and SortStructuredList(). Sorting of linked lists is stable and uses a merge-sort, while sorting for arrays is unstable and uses a quicksort.

do #=1while @.# \==''/*find how many entries in list. */end/*#*/#=#-1/*adjust because of DO increment.*/return/*──────────────────────────────────SHOW@ subroutine────────────────────*/show@: do j=1for # /* [↓] display all list elements*/say' element'right(j,length(#))arg(1)':' @.jend/*j*/saycopies('■',50)/*show a separator line. */return

output using the default list:

element 1 before sort: UK London
element 2 before sort: US New York
element 3 before sort: US Birmingham
element 4 before sort: UK Birmingham
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
element 1 after sort: UK Birmingham
element 2 after sort: UK London
element 3 after sort: US Birmingham
element 4 after sort: US New York
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

Ruby's built-in sort methods (Array#sort, Array#sort!, Array#sort_by!, Enumerable#sort and Enumerable#sort_by) are not stable. MRI uses quicksort, which is not stable (1). It seems that stable sorting is not worth the performance trade-off; MRI rejected a proposal to switch to a stable sort (2).

Other implementations of Ruby might differ. Old versions of JRuby used java.util.Arrays.sort, which was a stable sort, but was slower than MRI. To increase performance, JRuby switched to quicksort, which is not stable. (3)

There are two sort methods defined on Seq, which is the base collection trait for all sequences. The methods are sortWith and sortBy, and differ only on the argument used. The first expects a function that will implement the "less than" method for the type of the sequence. The second expects a function from the type of the sequence into any type for which there is an Ordering, plus an implicit Ordering of the proper type.

Besides that, there is the object scala.util.Sorting, which provides quickSort<tt> and <tt>stableSort. The former is only provided on Array, but the latter is provided over both Array and Seq. These sorts operate in-place, with the one over Seq returning a sorted Array. Here is one example: