Sort stability: Difference between revisions
Content added Content deleted
(→{{header|Pascal}}: give status) |
(Move REXX sample to ooRexx as it's really written for ooRexx.) |
||
Line 371: | Line 371: | ||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
OCaml's [http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html#VALsort List.sort] and [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html#VALsort Array.sort] functions are not guaranteed to be stable. The stable versions are [http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html#VALstable_sort List.stable_sort] and [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html#VALstable_sort Array.stable_sort], respectively. |
OCaml's [http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html#VALsort List.sort] and [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html#VALsort Array.sort] functions are not guaranteed to be stable. The stable versions are [http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html#VALstable_sort List.stable_sort] and [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Array.html#VALstable_sort Array.stable_sort], respectively. |
||
=={{header|ooRexx}}== |
|||
Open Object Rexx provides sort methods (<code>sort</code> and <code>sortWith(comparator)</code>) for its collection classes. By default these sort methods are implemented via an unstable <em>Quicksort</em> algorithm but the language does provide stable sorting methods (<code>stableSort</code> and <code>stableSortWith(comparator)</code>) implemented via a stable <em>Mergesort</em> algorithm. |
|||
<lang ooRexx>/* Rexx */ |
|||
Do |
|||
cities = .array~of('UK London', 'US New York', 'US Birmingham', 'UK Birmingham',) |
|||
Say; Say 'Original table' |
|||
Call display cities |
|||
Say; Say 'Unstable sort on city' |
|||
sorted = cities~copy |
|||
sorted~sortWith(.ColumnComparator~new(4, 20)) |
|||
Call display sorted |
|||
Say; Say 'Stable sort on city' |
|||
sorted = cities~copy |
|||
sorted~stableSortWith(.ColumnComparator~new(4, 20)) |
|||
Call display sorted |
|||
Say; Say 'Unstable sort on country' |
|||
sorted = cities~copy |
|||
sorted~sortWith(.ColumnComparator~new(1, 2)) |
|||
Call display sorted |
|||
Say; Say 'Stable sort on country' |
|||
sorted = cities~copy |
|||
sorted~stableSortWith(.ColumnComparator~new(1, 2)) |
|||
Call display sorted |
|||
Return |
|||
End |
|||
Exit |
|||
display: Procedure |
|||
Do |
|||
Use arg CT |
|||
Say '-'~copies(80) |
|||
Loop c_ over CT |
|||
Say c_ |
|||
End c_ |
|||
Return |
|||
End |
|||
Exit |
|||
</lang> |
|||
;Output |
|||
<pre> |
|||
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 |
|||
</pre> |
|||
=={{header|OpenEdge/Progress}}== |
=={{header|OpenEdge/Progress}}== |
||
Line 525: | Line 609: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
A Rexx implementation of this task can be found here in the [[#ooRexx|ooRexx]] solution. |
|||
Open Object Rexx provides sort methods (<code>sort</code> and <code>sortWith(comparator)</code>) for its collection classes. By default these sort methods are implemented via an unstable <em>Quicksort</em> algorithm but the language does provide stable sorting methods (<code>stableSort</code> and <code>stableSortWith(comparator)</code>) implemented via a stable <em>Mergesort</em> algorithm. |
|||
<lang REXX>/* Rexx */ |
|||
Do |
|||
cities = .array~of('UK London', 'US New York', 'US Birmingham', 'UK Birmingham',) |
|||
Say; Say 'Original table' |
|||
Call display cities |
|||
Say; Say 'Unstable sort on city' |
|||
sorted = cities~copy |
|||
sorted~sortWith(.ColumnComparator~new(4, 20)) |
|||
Call display sorted |
|||
Say; Say 'Stable sort on city' |
|||
sorted = cities~copy |
|||
sorted~stableSortWith(.ColumnComparator~new(4, 20)) |
|||
Call display sorted |
|||
Say; Say 'Unstable sort on country' |
|||
sorted = cities~copy |
|||
sorted~sortWith(.ColumnComparator~new(1, 2)) |
|||
Call display sorted |
|||
Say; Say 'Stable sort on country' |
|||
sorted = cities~copy |
|||
sorted~stableSortWith(.ColumnComparator~new(1, 2)) |
|||
Call display sorted |
|||
Return |
|||
End |
|||
Exit |
|||
display: Procedure |
|||
Do |
|||
Use arg CT |
|||
Say '-'~copies(80) |
|||
Loop c_ over CT |
|||
Say c_ |
|||
End c_ |
|||
Return |
|||
End |
|||
Exit |
|||
</lang> |
|||
;Output |
|||
<pre> |
|||
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 |
|||
</pre> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |