Jump to content

Sorting algorithms/Cycle sort: Difference between revisions

(Add NetRexx)
(→‎REXX version 1: corrected)
Line 581:
<lang rexx>/* REXX ***************************************************************
* 12.06.2014 Walter Pachl translated from Wikipedia's code
* 20.06.2014 WP corrected (courtesy Alan Sampson)
**********************************************************************/
list='1 9 3 5 8 4 7 0 6 2'
Line 588 ⟶ 589:
End
Say list
Call writes=cyclesort()
Say 'writes='writes
ol=''
Do i=0 To n-1
Line 596 ⟶ 598:
Exit
 
cycleSort: procedure expose array. n
writes = 0
-- Loop through the array to find cycles to rotate.
do cycleStart=0 to n-12
item = array.cycleStart
 
-- Find where to put the item.
pos = cycleStart
Do i=cycleStart+1 to n+-1
if array.i < item Then
pos += 1
End
 
-- If the item is already there, this is not a cycle.
if pos == cycleStart Then
Iterate
 
-- Otherwise, put the item there or right after any duplicates.
Do while item == array.pos
pos += 1
Line 612 ⟶ 621:
Parse Value array.pos item With item array.pos
writes += 1
 
-- Rotate the rest of the cycle.
Do while pos <> cycleStart
 
-- Find where to put the item.
pos = cycleStart
Do i=cycleStart + 1 to n-1
if array.i < item Then
pos += 1
End
 
-- Put the item there or right after any duplicates.
Do while item == array.pos
pos += 1
Line 625 ⟶ 640:
End
End
Sayreturn 'writes='writes</lang>
return</lang>
{{out}}
<pre>1 9 3 5 8 4 7 0 6 2
2,299

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.