1234567891011121314151617181920212223242526272829 |
- #!/usr/bin/ruby
- #
- ## https://rosettacode.org/wiki/Sorting_Algorithms/Circle_Sort
- #
- func circlesort(arr, beg=0, end=arr.end) {
- var swaps = 0
- if (beg < end) {
- var (lo, hi) = (beg, end)
- do {
- if (arr[lo] > arr[hi]) {
- arr.swap(lo, hi)
- ++swaps
- }
- ++hi if (--hi == ++lo)
- } while (lo < hi)
- swaps += circlesort(arr, beg, hi)
- swaps += circlesort(arr, lo, end)
- }
- return swaps
- }
- var numbers = %n(2 3 3 5 5 1 1 7 7 6 6 4 4 0 0)
- do { say numbers } while circlesort(numbers)
- var strs = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane", "Alice"]
- do { say strs } while circlesort(strs)
|