-
Notifications
You must be signed in to change notification settings - Fork 0
/
dutchflag.go
47 lines (44 loc) · 1.16 KB
/
dutchflag.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
package arrays
// DutchflagSubarrays reorders slice so that all elements
// less than s[i] appear first, followed by elements equal to s[i],
// followed by elements greater than s[i].
// The time complexity is O(n) and space complexity is O(n).
func DutchflagSubarrays(s []int, p int) []int {
var r, w, b []int
for _, v := range s {
if v < s[p] {
r = append(r, v)
} else if v == s[p] {
w = append(w, v)
} else {
b = append(b, v)
}
}
s = append(r, w...)
return append(s, b...)
}
// DutchflagTwoPasses reorders slice so that all elements
// less than s[i] appear first, followed by elements equal to s[i],
// followed by elements greater than s[i].
// The time complexity is O(n) and space complexity is O(1).
func DutchflagTwoPasses(s []int, p int) []int {
pivot, smaller := s[p], 0
// First pass: group elements smaller than pivot
for i := range s {
if s[i] < pivot {
s[i], s[smaller] = s[smaller], s[i]
smaller++
}
}
// Second pass: group elements larger than pivot
larger := len(s) - 1
for i := len(s) - 1; i >= 0; i-- {
if s[i] < pivot {
break
} else if s[i] > pivot {
s[i], s[larger] = s[larger], s[i]
larger--
}
}
return s
}