mirror of
https://github.com/0xAX/go-algorithms
synced 2024-11-13 13:10:30 +00:00
35 lines
524 B
Go
35 lines
524 B
Go
package main
|
|
|
|
/*
|
|
* Odd-Even sort - https://en.wikipedia.org/wiki/Odd-even_sort
|
|
*/
|
|
|
|
func OddEvenSort(arr []int) {
|
|
tmp, isSorted := 0, false
|
|
|
|
for isSorted == false {
|
|
|
|
isSorted = true
|
|
|
|
for i := 1; i < len(arr) - 1; i = i + 2 {
|
|
if arr[i] > arr[i + 1] {
|
|
tmp = arr[i]
|
|
arr[i] = arr[i + 1]
|
|
arr[i + 1] = tmp
|
|
|
|
isSorted = false
|
|
}
|
|
}
|
|
|
|
for i := 0; i < len(arr) - 1; i = i + 2 {
|
|
if arr[i] > arr[i + 1] {
|
|
tmp = arr[i]
|
|
arr[i] = arr[i + 1]
|
|
arr[i + 1] = tmp
|
|
|
|
isSorted = false
|
|
}
|
|
}
|
|
}
|
|
}
|