Sort stability: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|Oz}}: Added example) |
||
Line 74: | Line 74: | ||
Oz' [http://www.mozart-oz.org/home/doc/base/list.html#label295 Sort] function is not guaranteed to be stable in the documentation. |
Oz' [http://www.mozart-oz.org/home/doc/base/list.html#label295 Sort] function is not guaranteed to be stable in the documentation. |
||
However, it uses |
However, internally it uses [[Merge sort]] and in practice ''is'' stable '''if''' a reflexive ordering is used, e.g. <code>Value.'=<'</code> or <code>Value.'>='</code>. |
||
Example: |
|||
<lang oz>declare |
|||
Cities = ['UK'#'London' |
|||
'US'#'New York' |
|||
'US'#'Birmingham' |
|||
'UK'#'Birmingham'] |
|||
in |
|||
%% sort by city; stable because '=<' is reflexiv |
|||
{Show {Sort Cities fun {$ A B} A.2 =< B.2 end}} |
|||
%% sort by country; NOT stable because '<' is not reflexiv |
|||
{Show {Sort Cities fun {$ A B} A.1 < B.1 end}}</lang> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |