forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.go
More file actions
34 lines (28 loc) · 688 Bytes
/
QuickSort.go
File metadata and controls
34 lines (28 loc) · 688 Bytes
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
package quick-sort
func sort(arr []int) []int {
var recurse func(left int, right int)
var partition func(left int, right int, pivot int) int
partition = func(left int, right int, pivot int) int {
v := arr[pivot]
right--
arr[pivot], arr[right] = arr[right], arr[pivot]
for i := left; i < right; i++ {
if arr[i] <= v {
arr[i], arr[left] = arr[left], arr[i]
left++
}
}
arr[left], arr[right] = arr[right], arr[left]
return left
}
recurse = func(left int, right int) {
if left < right {
pivot := (right + left) / 2
pivot = partition(left, right, pivot)
recurse(left, pivot)
recurse(pivot+1, right)
}
}
recurse(0, len(arr))
return arr
}