Sort stability: Difference between revisions
→{{header|REXX}}: replace the wrong program
Drkameleon (talk | contribs) (added Arturo) |
Walterpachl (talk | contribs) (→{{header|REXX}}: replace the wrong program) |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 1,532:
=={{header|REXX}}==
<syntaxhighlight lang="rexx"></syntaxhighlight>
call
call show 'before
call stableSort
exit
/*----------------------------------------------------------------------------*/
stableSort: procedure expose a.
parse Value '' With l1 l2
Do i=1 To a.0
parse Var a.i f1 f2
f2=translate(f2,'_',' ')
If pos(f1,l1)=0 Then l1=l1 f1
If pos(f2,l2)=0 Then l2=l2 f2
End
l1=wordsort(l1)
l2=wordsort(l2)
Say ''
Say 'sorted by country'
Do While l1<>''
Parse Var l1 f1s l1
Do i=1 To a.0
parse Var a.i f1 f2
If f1=f1s Then
Say a.i
End
End
Say ''
Say 'sorted by city'
Do While l2<>''
Parse Var l2 f2s l2
Do i=1 To a.0
parse Var a.i f1 f2
If translate(f2,'_',' ')=f2s Then
Say a.i
End
End
/*---------------------------------------------------------------------------------*/
gena: a.0=0
Call store 'UK London'
Call store 'US New York'
Call store 'US Birmingham'
Call store 'UK Birmingham'
Return
store:
z=a.0+1
a.z=arg(1)
a.0=z
Return
show:
Say arg(1)
do i=1 To a.0
say a.i
End
Return
wordsort: Procedure
/**********************************************************************
* Sort the list of words supplied as argument. Return the sorted list
**********************************************************************/
Parse Arg wl
wa.=''
wa.0=0
Do While wl<>''
Parse Var wl w wl
Do i=1 To wa.0
If wa.i>w Then Leave
End
If i<=wa.0 Then Do
Do j=wa.0 To i By -1
ii=j+1
wa.ii=wa.j
End
End
wa.i=w
wa.0=wa.0+1
End
swl=''
Do i=1 To wa.0
swl=swl wa.i
End
Return strip(swl)</syntaxhighlight>
{{out|output|text= when using the default list:}}
<pre>
K:\>rexx sso
UK London
US New York
US Birmingham
sorted by country
UK London
UK Birmingham
US New York
US Birmingham
sorted by city
US Birmingham
UK Birmingham
UK London
US New York
</pre>
Line 1,703 ⟶ 1,771:
scala> cities.sortBy(_ substring 4)
res47: Seq[String] = ArrayBuffer(US Birmingham, UK Birmingham, UK London, US New York)</syntaxhighlight>
Besides that, there is the object <tt>scala.util.Sorting</tt>, which provides <tt>quickSort</tt> and <tt>stableSort</tt>. The former is only provided on <tt>Array</tt>, but the latter is provided over both <tt>Array</tt> and <tt>Seq</tt>. These sorts operate in-place, with the one over <tt>Seq</tt> returning a sorted <tt>Array</tt>. Here is one example:
<syntaxhighlight lang="scala">scala> val cityArray = cities.toArray
cityArray: Array[String] = Array(UK London, US New York, US Birmingham, UK Birmingham)
Line 1,738 ⟶ 1,806:
=={{header|TXR}}==
TXR provides a number of sorting functions. <code>sort</code> and <code>nsort</code> (destructive variant) are not stable for vectors and strings, but are stable for lists.
The functions <code>ssort</code> and <code>snsort</code> counterparts are stable for all sequence kinds.
In addition, there are caching variants of all these functions: <code>csort</code>, <code>cnsort</code>, <code>cssort</code> and <code>csnsort</code>. They respectively have the same stability properties as their counterparts without the leading <code>c</code>.
TXR Lisp originally had one sorting function called <code>sort</code>, which was destructive, like the <code>sort</code> in Common Lisp. That function was renamed to <code>nsort</code>, and <code>sort</code> became the name of a non-destructive function. That happened in TXR 238, released in May, 2020.
=={{header|Wren}}==
Line 1,747 ⟶ 1,819:
Below we illustrate the points made in the task description using the stable insertion sort.
<syntaxhighlight lang="
var data = [ ["UK", "London"], ["US", "New York"], ["US", "Birmingham"], ["UK", "Birmingham"] ]
Line 1,790 ⟶ 1,862:
[US, New York]
</pre>
=={{header|XPL0}}==
There is no built-in sort routine in XPL0. The 32-bit versions are
distributed with xpllib, which provides an integer sort routine. This
uses the Quicksort algorithm, which is unstable.
=={{header|zkl}}==
|