-
Notifications
You must be signed in to change notification settings - Fork 373
/
dutch_flag_test.go
72 lines (60 loc) · 1.28 KB
/
dutch_flag_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
Problem:
- Given an array containing 0s, 1s and 2s, sort the array in-place.
Example:
- Input: []int{1, 0, 2, 1, 0}
Output: []int{0, 0, 1, 1, 2}
Approach:
- Have one pointer start at the beginning and the other at the end
while iterating through the array.
- We will move all 0s before that start pointer and 2s after the end
pointer so that all 1s would be between in the end.
Cost:
- O(n) time, O(1) space.
*/
package gtci
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestDutchFlagSort(t *testing.T) {
tests := []struct {
in []int
expected []int
}{
{[]int{0, 0, 0, 0, 0}, []int{0, 0, 0, 0, 0}},
{[]int{1, 1, 1, 1, 1}, []int{1, 1, 1, 1, 1}},
{[]int{2, 2, 2, 2, 2}, []int{2, 2, 2, 2, 2}},
{[]int{1, 0, 2, 1, 0}, []int{0, 0, 1, 1, 2}},
}
for _, tt := range tests {
dutchFlagSort(tt.in)
common.Equal(
t,
tt.expected,
tt.in,
)
}
}
func dutchFlagSort(a []int) {
start := 0
end := len(a) - 1
i := 0
for i <= end {
if a[i] == 0 {
// swap a[i] and a[start].
common.Swap(a, i, start)
// increment start pointer and i.
i++
start++
} else if a[i] == 1 {
// increment i pointer only.
i++
} else if a[i] == 2 {
// swap a[i] and a[end].
common.Swap(a, i, end)
// decrement end pointer only.
end--
}
}
}