forked from hoanhan101/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble_sort_test.go
58 lines (49 loc) · 1.24 KB
/
bubble_sort_test.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
/*
Problem:
- Implement bubble sort.
Approach:
- Repeatedly swap the adjacent elements if they are in the wrong order in the
array, one item at a time.
Cost:
- O(n^2) time and O(1) space.
*/
package lab
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestBubbleSort(t *testing.T) {
tests := []struct {
in []int
expected []int
}{
{[]int{}, []int{}},
{[]int{1}, []int{1}},
{[]int{1, 2}, []int{1, 2}},
{[]int{2, 1}, []int{1, 2}},
{[]int{2, 1, 3}, []int{1, 2, 3}},
{[]int{1, 1, 1}, []int{1, 1, 1}},
{[]int{2, 1, 2}, []int{1, 2, 2}},
{[]int{1, 2, 4, 3, 6, 5}, []int{1, 2, 3, 4, 5, 6}},
{[]int{6, 2, 4, 3, 1, 5}, []int{1, 2, 3, 4, 5, 6}},
{[]int{6, 5, 4, 3, 2, 1}, []int{1, 2, 3, 4, 5, 6}},
}
for _, tt := range tests {
bubbleSort(tt.in)
common.Equal(t, tt.expected, tt.in)
}
}
func bubbleSort(in []int) {
length := len(in)
// for each element in the list, check it with almost every other element.
for i := 0; i < length; i++ {
// since the last i element is already in place, only iterate through
// the item before the last one.
for j := 0; j < length-i-1; j++ {
// swap the adjacent elements if they are not in ascending order.
if in[j] > in[j+1] {
common.Swap(in, j, j+1)
}
}
}
}