-
Notifications
You must be signed in to change notification settings - Fork 67
/
bubble-sort.go
57 lines (54 loc) · 1.15 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
/*
Time complexity: O(N^2)
*/
package sort
func SimpleBubbleSort(array []int) {
checkArray(array)
for i := 0; i < len(array); i++ {
for j := 1; j < len(array) - i; j++ {
if array[j - 1] > array[j] {
swap(array, j - 1, j)
}
}
}
}
func FlagSwapBubbleSort(array []int) {
checkArray(array)
var has_swapped bool
for i := 0; i < len(array); i++ {
has_swapped = false
for j := 1; j < len(array) - i; j++ {
if array[j - 1] > array[j] {
swap(array, j - 1, j)
has_swapped = true
}
}
// if the last scanning has no swapping, the array is sorted.
// Then there's no need to scan again.
if !has_swapped {
break
}
}
}
func FlagSwapPositionBubbleSort(array []int) {
checkArray(array)
var has_swapped bool
var flag int
last_swap_position := len(array)
for i := 0; i < len(array); i++ {
// After the last swapping at x, [x, n] is sorted.
// So we just need to sort [0, x] in the next scanning.
flag = last_swap_position
has_swapped = false
for j := 1; j < flag; j++ {
if array[j - 1] > array[j] {
swap(array, j - 1, j)
has_swapped = true
last_swap_position = j
}
}
if !has_swapped {
break
}
}
}