OddEvenSort performs the odd-even sort algorithm on the given array. It is a variation of bubble sort that compares adjacent pairs, alternating between odd and even indexed elements in each pass until the array is sorted.
(arr []T)
| 10 | // It is a variation of bubble sort that compares adjacent pairs, alternating |
| 11 | // between odd and even indexed elements in each pass until the array is sorted. |
| 12 | func OddEvenSort[T constraints.Ordered](arr []T) []T { |
| 13 | if len(arr) == 0 { // handle empty array |
| 14 | return arr |
| 15 | } |
| 16 | |
| 17 | swapped := true |
| 18 | for swapped { |
| 19 | swapped = false |
| 20 | |
| 21 | // Perform "odd" indexed pass |
| 22 | for i := 1; i < len(arr)-1; i += 2 { |
| 23 | if arr[i] > arr[i+1] { |
| 24 | arr[i], arr[i+1] = arr[i+1], arr[i] |
| 25 | swapped = true |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // Perform "even" indexed pass |
| 30 | for i := 0; i < len(arr)-1; i += 2 { |
| 31 | if arr[i] > arr[i+1] { |
| 32 | arr[i], arr[i+1] = arr[i+1], arr[i] |
| 33 | swapped = true |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return arr |
| 39 | } |
nothing calls this directly
no outgoing calls
no test coverage detected