-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathbubble_sort.go
More file actions
35 lines (31 loc) · 753 Bytes
/
bubble_sort.go
File metadata and controls
35 lines (31 loc) · 753 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
package main
import "fmt"
// Iterative
func BubbleSortIterative(slice []int) {
for index1 := len(slice) - 1; index1 > 0; index1-- {
for index2 := 0; index2 < index1; index2++ {
if slice[index2] > slice[index2+1] {
slice[index2], slice[index2+1] = slice[index2+1], slice[index2]
}
}
}
}
// Recursive
func BubbleSortRecursive(slice []int, size int) {
swaps := 0
for index := 0; index < size-1; index++ {
if slice[index] > slice[index+1] {
slice[index], slice[index+1] = slice[index+1], slice[index]
swaps++
}
}
if swaps != 0 {
BubbleSortRecursive(slice, size-1)
}
}
func main() {
slice := []int{5, 2, 1, 6, 9, 8, 7, 3, 4}
fmt.Println("Slice:", slice)
BubbleSortIterative(slice)
fmt.Println("BubbleSort:", slice)
}