doSort is the recursive function that implements the circle sort algorithm.
(arr []T, left, right int)
| 15 | |
| 16 | // doSort is the recursive function that implements the circle sort algorithm. |
| 17 | func doSort[T constraints.Ordered](arr []T, left, right int) bool { |
| 18 | if left == right { |
| 19 | return false |
| 20 | } |
| 21 | swapped := false |
| 22 | low := left |
| 23 | high := right |
| 24 | |
| 25 | for low < high { |
| 26 | if arr[low] > arr[high] { |
| 27 | arr[low], arr[high] = arr[high], arr[low] |
| 28 | swapped = true |
| 29 | } |
| 30 | low++ |
| 31 | high-- |
| 32 | } |
| 33 | |
| 34 | if low == high && arr[low] > arr[high+1] { |
| 35 | arr[low], arr[high+1] = arr[high+1], arr[low] |
| 36 | swapped = true |
| 37 | } |
| 38 | |
| 39 | mid := left + (right-left)/2 |
| 40 | leftHalf := doSort(arr, left, mid) |
| 41 | rightHalf := doSort(arr, mid+1, right) |
| 42 | |
| 43 | return swapped || leftHalf || rightHalf |
| 44 | } |