-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.go
57 lines (49 loc) · 986 Bytes
/
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
package main
import "fmt"
func partition(list []int, start, end int) int {
bottom := start - 1
top := end
pivot := list[end]
done := false
for done == false {
for done == false {
bottom += 1
if bottom == top {
done = true
break
}
if list[bottom] > pivot {
list[top] = list[bottom]
break
}
}
for done == false {
top -= 1
if top == bottom {
done = true
break
}
if list[top] < pivot {
list[bottom] = list[top]
break
}
}
}
list[top] = pivot
return top
}
func quicksort(list []int, start, end int) {
if start < end {
split := partition(list, start, end)
quicksort(list, start, split - 1)
quicksort(list, split + 1, end)
} else {
return
}
}
func main() {
unsorted := []int{5,1,4,2,3}
fmt.Println("Unsorted =", unsorted)
quicksort(unsorted, 0, len(unsorted) - 1)
fmt.Println("Sorted =", unsorted)
}