Jump to content

Sort stability: Difference between revisions

→‎{{header|REXX}}: remove dummy (non-entity) entry that referred to an ooRexx entry (as it didn't include a program that Classic REXX could use), added a Classic REXX entry.
m (Added Sidef language.)
(→‎{{header|REXX}}: remove dummy (non-entity) entry that referred to an ooRexx entry (as it didn't include a program that Classic REXX could use), added a Classic REXX entry.)
Line 745:
 
=={{header|REXX}}==
Classic REXX has no built-in routines for sorting, so this programming example uses a classic ''bubble sort''   (which is stable).
A Rexx implementation of this task can be found here in the [[#ooRexx|ooRexx]] solution.
<lang rexx>/*REXX program sorts an array using a (stable) bubble-sort algorithm.*/
call gen@ /*generate the array elements. */
call show@ 'before sort' /*show the before array elements.*/
call bubbleSort # /*invoke the bubble sort. */
call show@ ' after sort' /*show the after array elements.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────BUBBLESORT subroutine───────────────*/
bubbleSort: procedure expose @.; parse arg n /*N: number of items.*/
/*diminish # items each time. */
do until done /*sort until it's done. */
done=1 /*assume it's done (1 ≡ true). */
do j=1 for n-1 /*sort M items this time around. */
k=j+1 /*point to the next item. */
if @.j>@.k then do /*is it out of order? */
_=@.j /*assign to a temp variable. */
@.j=@.k /*swap current item with next ···*/
@.k=_ /* ··· and the next with _ */
done=0 /*indicate it's not done, whereas*/
end /* [↑] 1≡true 0≡false */
end /*j*/
end /*until*/
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: @. = /*assign default value to all @. */
@.1 = 'UK London'
@.2 = 'US New York'
@.3 = 'US Birmingham'
@.4 = 'UK Birmingham'
 
do #=1 while @.# \=='' /*find how many entries in list. */
end /*#*/
#=#-1 /*adjust because of DO increment.*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: do j=1 for # /* [↓] display all list elements*/
say ' element' right(j,length(#)) arg(1)':' @.j
end /*j*/
say copies('■',50) /*show a separator line. */
return</lang>
'''output''' &nbsp; using the default list:
<pre>
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
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
</pre>
 
=={{header|Ruby}}==
Cookies help us deliver our services. By using our services, you agree to our use of cookies.