Sort stability: Difference between revisions
→{{header|REXX}}: replace the wrong program
(→{{header|TXR}}: Update with new info.) |
Walterpachl (talk | contribs) (→{{header|REXX}}: replace the wrong program) |
||
(2 intermediate revisions by one other user 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,751 ⟶ 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"] ]
|