Sorting algorithms/Cycle sort: Difference between revisions

m
{{out}}
No edit summary
m ({{out}})
Line 332:
=={{header|J}}==
 
J's sort is natively a single write sort, but it assigns the whole array at once. It would be trivial do the writes one at a time, and to avoid updating values which are not changed:
It would be trivial do the writes one at a time, and to avoid updating values which are not changed:
 
<lang J>noncyc=:3 :0
Line 346 ⟶ 347:
)</lang>
 
{{out|Example use:}}
 
<lang J> noncyc 9 8 15 17 4 0 1 2 17 9 3 12 11 12 19 15 3 9 16 9
17 writes
0 1 2 3 3 4 8 9 9 9 9 11 12 12 15 15 16 17 17 19</lang>
 
Meanwhile, if we just wanted the "value at a time swapping" mechanism, an idiomatic approach might look something like this:
an idiomatic approach might look something like this:
 
<lang j>cyc0=:3 :0
Line 371 ⟶ 372:
)</lang>
 
{{out|Example use:}}
 
<lang J> cyc0 9 8 15 17 4 0 1 2 17 9 3 12 11 12 19 15 3 9 16 9
18 writes
Line 379:
This gives us an extra write, because we're using a generic cycle abstraction.
 
Also that's still a bit different from the wikipedia algorithm. We might model the wikipedia algorithm like this:
We might model the wikipedia algorithm like this:
 
<lang J>cyc1=:3 :0
Line 407 ⟶ 408:
)</lang>
 
{{out|Example use:}}
 
<lang J> cyc1 9 8 15 17 4 0 1 2 17 9 3 12 11 12 19 15 3 9 16 9
17 writes
Line 478:
}</lang>
 
{{out}}
Output:
 
<pre>[5, 0, 1, 2, 2, 3, 5, 1, 1, 0, 5, 6, 9, 8, 0, 1]
[0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 5, 5, 5, 6, 8, 9]
Line 934 ⟶ 933:
_=@.1; do j=2 to #; _=_ @.j; end; say ' sorted list: ' _
return writes</lang>
'''output'''{{out}} using the default input:
<pre>
unsorted list: -3.14 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 2 8 8 4
Line 940 ⟶ 939:
and took 34 writes.
</pre>
'''output'''{{out}} using the input of: &nbsp; <tt> FM Stereo has been around since 1961. </tt>
<pre>
unsorted list: FM Stereo has been around since 1961.
Line 947 ⟶ 946:
</pre>
Note (for the above output). &nbsp; This REXX program was executed on an ASCII machine.
<br>On an &nbsp; ASCII &nbsp; machine, the order of sorting is numbers, uppercase letters, lowercase letters.<br>
<br>On an EBCDIC machine, the order of sorting is lowercase letters, uppercase letters, numbers.<br>
<br>Other (special) characters are also in a different order.
 
===version 3===
Line 1,159 ⟶ 1,158:
return $writes
}</lang>
'''Demonstrating:'''
<lang tcl>set example {0 1 2 2 2 2 1 9 3.5 5 8 4 7 0 6}
puts "Data was: $example"
Anonymous user