Sorting algorithms/Cycle sort: Difference between revisions

Content deleted Content added
Added solution for Action!
Galileo (talk | contribs)
Line 2,416:
Writes : 6
</pre>
 
=={{header|Yabasic}}==
{{trans|FreeBASIC}}
<lang Yabasic>// Rosetta Code problem: http://rosettacode.org/wiki/Cycle_sort
// by Galileo, 04/2022
 
sub cyclesort(array())
local writes, lenarray, item, pos, i, prov
lenarray = arraysize(array(), 1)
for cyclestart = 1 to lenarray
item = array(cyclestart)
pos = cyclestart
for i = cyclestart + 1 to lenarray
if array(i) < item pos = pos + 1
next
if pos = cyclestart continue
while item = array(pos)
pos = pos + 1
wend
prov = array(pos) : array(pos) = item : item = prov
writes = writes + 1
while pos != cyclestart
pos = cyclestart
for i = cyclestart + 1 to lenarray
if array(i) < item pos = pos + 1
next
while item = array(pos)
pos = pos + 1
wend
prov = array(pos) : array(pos) = item : item = prov
writes = writes + 1
wend
next
return writes
end sub
 
sub printArray(array())
local i
for i = 1 to arraysize(array(), 1)
print array(i), " ";
next
print
end sub
 
 
dim ints(10)
 
for i = 1 to 10
ints(i) = int(ran(10))
next
 
printArray(ints())
print "writes ", cyclesort(ints())
printArray(ints())</lang>
{{out}}
<pre>9 9 9 6 8 1 9 7 3 4
writes 8
1 3 4 6 7 8 9 9 9 9
---Program done, press RETURN---</pre>