Jump to content

Sorting Algorithms/Circle Sort: Difference between revisions

no edit summary
(Added solution for Action!)
No edit summary
Line 2,630:
PRINT
RETURN</lang>
 
=={{header|Vlang}}==
{{trans|go}}
<lang vlang>fn circle_sort(mut a []int, l int, h int, s int) int {
mut hi := h
mut lo := l
mut swaps := s
if lo == hi {
return swaps
}
high, low := hi, lo
mid := (hi - lo) / 2
for lo < hi {
if a[lo] > a[hi] {
a[lo], a[hi] = a[hi], a[lo]
swaps++
}
lo++
hi--
}
if lo == hi {
if a[lo] > a[hi+1] {
a[lo], a[hi+1] = a[hi+1], a[lo]
swaps++
}
}
swaps = circle_sort(mut a, low, low+mid, swaps)
swaps = circle_sort(mut a, low+mid+1, high, swaps)
return swaps
}
fn main() {
aa := [
[6, 7, 8, 9, 2, 5, 3, 4, 1],
[2, 14, 4, 6, 8, 1, 3, 5, 7, 11, 0, 13, 12, -1],
]
for a1 in aa {
mut a:=a1.clone()
println("Original: $a")
for circle_sort(mut a, 0, a.len-1, 0) != 0 {
// empty block
}
println("Sorted : $a\n")
}
}</lang>
 
{{out}}
<pre>
Original: [6, 7, 8, 9, 2, 5, 3, 4, 1]
Sorted : [1, 2, 3, 4, 5, 6, 7, 8, 9]
 
Original: [2, 14, 4, 6, 8, 1, 3, 5, 7, 11, 0, 13, 12, -1]
Sorted : [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 11, 12, 13, 14]
</pre>
 
=={{header|Wren}}==
338

edits

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