Sorting algorithms/Cycle sort: Difference between revisions

Added 11l
m (added Category:Sorting)
(Added 11l)
Line 21:
* [http://www.youtube.com/watch?v=ZSJGf5Ngw18 Youtube] Visualization and audibilization of Cycle Sort algorithm.
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<lang 11l>F cycleSort(&vector)
V writes = 0
 
L(=item) vector
V cycleStart = L.index
V pos = cycleStart
L(item2) vector[cycleStart+1..]
I item2 < item
pos++
 
I pos == cycleStart
L.continue
 
L item == vector[pos]
pos++
swap(&vector[pos], &item)
writes++
 
L pos != cycleStart
pos = cycleStart
L(item2) vector[cycleStart+1..]
I item2 < item
pos++
 
L item == vector[pos]
pos++
swap(&vector[pos], &item)
writes++
 
R writes
 
V x = [Float(0), 1, 2, 2, 2, 2, 1, 9, 3.5, 5, 8, 4, 7, 0, 6]
V xcopy = copy(x)
V writes = cycleSort(&xcopy)
I xcopy != sorted(x)
print(‘Wrong order!’)
E
print("#.\nIs correctly sorted using cycleSort to".format(x))
print("#.\nUsing #. writes.".format(xcopy, writes))</lang>
 
{{out}}
<pre>
[0, 1, 2, 2, 2, 2, 1, 9, 3.5, 5, 8, 4, 7, 0, 6]
Is correctly sorted using cycleSort to
[0, 0, 1, 1, 2, 2, 2, 2, 3.5, 4, 5, 6, 7, 8, 9]
Using 10 writes.
</pre>
 
=={{header|360 Assembly}}==
1,480

edits