-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbubble-sort.go
59 lines (52 loc) · 1.38 KB
/
bubble-sort.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Date: 2023-12-28
Description:
Implement bubble sort.
Approach:
Compares adjacent elements and pushes to it's extreme end. While
sorting in ascending order largest element reaches at last place after first
iteration of outer loop. For descending smallest reaches to the last place.
Complexity:
O(n^2)
*/
package main
import (
"fmt"
"reflect"
)
// Sorts slice in increasing order using bubble sort.
func bubbleSort(nums []int) {
swap := true
for i := 0; i < len(nums); i++ {
swap = false
for j := 0; j < len(nums)-1-i; j++ {
if nums[j] > nums[j+1] { // For descending order, reverse this condition.
nums[j], nums[j+1] = nums[j+1], nums[j]
swap = true
}
}
if !swap {
break
}
}
}
func main() {
testCases := []struct {
input, expected []int
}{
{input: []int{5, 4, 3, 2, 1, 10, 28, 7, 6}, expected: []int{1, 2, 3, 4, 5, 6, 7, 10, 28}},
{input: []int{3, 4, 5, 2, 1}, expected: []int{1, 2, 3, 4, 5}},
{input: []int{3, 4, 5, 2, 1, 6}, expected: []int{1, 2, 3, 4, 5, 6}},
{input: []int{}, expected: []int{}},
{input: []int{1}, expected: []int{1}},
{input: []int{2, 1}, expected: []int{1, 2}},
}
for idx, tc := range testCases {
inputCopy := make([]int, len(tc.input))
copy(inputCopy, tc.input)
bubbleSort(inputCopy)
if !reflect.DeepEqual(inputCopy, tc.expected) {
fmt.Printf("\nTC %d failed. Expected %v, got %v", idx, tc.expected, inputCopy)
}
}
}