-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodd-even.go
More file actions
44 lines (40 loc) · 776 Bytes
/
odd-even.go
File metadata and controls
44 lines (40 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"fmt"
)
func oddeven(toSort []int) {
n := len(toSort)
for i := 0; i < n; i++ {
if i%2 != 0 {
for j := 2; j < n; j += 2 {
if toSort[j] < toSort[j-1] {
temp := toSort[j]
toSort[j] = toSort[j-1]
toSort[j-1] = temp
}
}
} else {
for j := 1; j < n; j += 2 {
if toSort[j] < toSort[j-1] {
temp := toSort[j]
toSort[j] = toSort[j-1]
toSort[j-1] = temp
}
}
}
}
}
func main() {
a := []int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
fmt.Println("Unsorted: ")
fmt.Println(a)
oddeven(a)
fmt.Println("Sorted via Selection Sort: ")
fmt.Println(a)
b := []int{0, 9, 3, 5, 4, 1, 6, 7, 8, 2}
fmt.Println("Unsorted: ")
fmt.Println(b)
oddeven(b)
fmt.Println("Sorted via Selection Sort: ")
fmt.Println(b)
}