-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.go
70 lines (66 loc) · 1.55 KB
/
QuickSort.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
package main
import "fmt"
type quickSort struct{}
func (q quickSort) Sort(numbers []int, debug bool) []int {
if debug {
fmt.Printf("=========================\nStart to handle %v\n", numbers)
}
if len(numbers) <= 1 {
return numbers
}
pivot := len(numbers) - 1
l := 0
r := pivot - 1
if debug {
fmt.Printf("pivot: %v(%v)\nl: %v(%v)\nr: %v(%v)\n", pivot, numbers[pivot], l, numbers[l], r, numbers[r])
}
for l < pivot {
if numbers[l] >= numbers[pivot] {
if debug {
fmt.Printf("l(hold): %v(%v)\n", l, numbers[l])
}
for l < r {
if numbers[r] < numbers[pivot] {
if debug {
fmt.Printf("swap: %v(%v) <==> %v(%v)\n", l, numbers[l], r, numbers[r])
}
numbers[l], numbers[r] = numbers[r], numbers[l]
if debug {
fmt.Printf("In-time array: %v\n", numbers)
}
break
} else {
r--
if debug {
fmt.Printf("r move: %v(%v)\n", r, numbers[r])
}
}
}
if l == r {
if debug {
fmt.Printf("r met l(hold) at: %v(%v)\nswap: %v(%v) <==> %v(%v)\n", l, numbers[l], l, numbers[l], pivot, numbers[pivot])
}
numbers[l], numbers[pivot] = numbers[pivot], numbers[l]
if debug {
fmt.Printf("In-time array: %v\n", numbers)
}
break
}
} else {
l++
if debug {
fmt.Printf("l move: %v(%v)\n", l, numbers[l])
}
}
}
if debug {
fmt.Printf("End an iteration, the pivot %v(%v) has been sorted.\n", l, numbers[l])
}
leftPart := numbers[:l]
q.Sort(leftPart, debug)
if l+1 < len(numbers) {
rightPart := numbers[l+1:]
q.Sort(rightPart, debug)
}
return numbers
}