Sorting algorithms/Cycle sort: Difference between revisions
Content added Content deleted
m (elided duplicate.) |
Drkameleon (talk | contribs) (Added Arturo implementation) |
||
Line 190: | Line 190: | ||
-31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782 |
-31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782 |
||
</pre> |
</pre> |
||
=={{header|Arturo}}== |
|||
<lang rebol>cycleSort: function [items][ |
|||
a: new items |
|||
position: 0 |
|||
loop 0..dec dec size a 'cycleStart [ |
|||
item: a\[cycleStart] |
|||
position: cycleStart |
|||
loop (cycleStart+1)..dec size a 'i [ |
|||
if (get a i) < item -> position: position + 1 |
|||
] |
|||
if position = cycleStart -> continue |
|||
while [item = a\[position]] -> position: position + 1 |
|||
tmp: a\[position] |
|||
a\[position]: item |
|||
item: tmp |
|||
while [position <> cycleStart][ |
|||
position: cycleStart |
|||
loop (cycleStart+1)..dec size a 'i [ |
|||
if a\[i] < item -> position: position + 1 |
|||
] |
|||
while [item = a\[position]] -> position: position + 1 |
|||
tmp: a\[position] |
|||
a\[position]: item |
|||
item: tmp |
|||
] |
|||
] |
|||
return a |
|||
] |
|||
print cycleSort [3 1 2 8 5 7 9 4 6]</lang> |
|||
{{out}} |
|||
<pre>1 2 3 4 5 6 7 8 9</pre> |
|||
=={{header|BCPL}}== |
=={{header|BCPL}}== |