2
0
mirror of https://github.com/0xAX/go-algorithms synced 2024-11-13 13:10:30 +00:00
go-algorithms/sorting/odd_even_sort.go
0xAX 46b32e8b0e * comb sort added
* odd-even sort added
   * heap sort added
2014-06-26 20:49:56 +06:00

45 lines
703 B
Go

package main
/*
* Odd-Even sort - https://en.wikipedia.org/wiki/Odd-even_sort
*/
import "fmt"
import "github.com/0xAX/go-algorithms"
func main() {
arr := utils.RandArray(10)
fmt.Println("Initial array is:", arr)
fmt.Println("")
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
}
}
}
fmt.Println("Sorted array is: ", arr)
}