Jump to content

Sort stability: Difference between revisions

m
→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations, simplified a subroutine.
m (→‎{{header|REXX}}: changed a comment.)
m (→‎{{header|REXX}}: changed/added comments and whitespace, changed indentations, simplified a subroutine.)
Line 853:
=={{header|REXX}}==
Classic REXX has no built-in routines for sorting, so this programming example uses a classic ''bubble sort''   (which is stable).
<lang rexx>/*REXX program sorts an a (stemmed) array using a (stable) bubble-sort bubble─sort algorithm. */
call gen@ /*generate the array elements. (strings)*/
call show@ 'before sort' /*show the before array elements. */
call bubbleSort # say copies('▒', 50) /*invoke the bubble sort. /*show a separator line between shows. */
call show@bubbleSort # ' after sort' /*showinvoke the bubble sort. after array elements.*/
exitcall show ' after sort' /*show the after /*stickarray a forkelements. in it, we're done.*/
#=#-1exit /*adjuststick becausea offork DOin incrementit, we're all done. */
/*──────────────────────────────────BUBBLESORT subroutine───────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
bubbleSort: procedure expose @.; parse arg n /*N: number of items.*/
bubbleSort: procedure expose @.; parse arg n /*N: is the number /*diminish #of items eachin timearray. */
do until done /*sort until it's done. /*diminish number of items each cycle. */
done=1 do until ok /*assumesort until it's donefinished sorting. (1 ≡ true). */
do j ok=1 for n-1 /*sortassume Mit's itemsdone this time(1≡true, around. 0≡false).*/
k=j+1 do j=1 for n-1 /*point to the next item./*sort N-1 items this time around. */
if @.j>@. k=j+1 then do /*is it out-of-order? /*point to the next item to be sorted. */
if @.j>@.k then do _=@.j /*assignis tothis aarray tempitem variable. out-of-order? */
@.j _=@.kj /*swapassign currentto itema withtemporary next ···variable (_). */
@.k=_ /* ··· and@.j=@.k the/*swap nextcurrent item with the next ··· _ */
done @.k=0_ /*indicate it's not done, whereas ··· and the next with temp var.*/
end /* [↑] ok=0 1≡true /*indicate it's not done, where ··· 0≡false */
end /*j [↑] 1≡true 0≡false */
end end /*untilj*/
end /*juntil*/
return
end /*#*/return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen@: @. = /*assign default value to all @. */
gen@: @.=; @.1 = 'UK London'
@.2 = 'US New York'
@.3 = 'US Birmingham'
@.4 = 'UK Birmingham'
do #=1 while @.# \==''; end; #=#-1 /*finddetermine how many entries in list. */
 
return
do #=1 while @.# \=='' /*find how many entries in list. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
end /*#*/
show: do j=1 for #; say ' element' right(j,length(#)) arg(1)':' @.j; end; return</lang>
#=#-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>
Line 898 ⟶ 891:
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>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.