forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Quick_Sort.go
46 lines (38 loc) · 832 Bytes
/
Quick_Sort.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
package main
import (
"fmt"
)
// Conquer
func Partition(array []int, left int, right int) int {
pivot := array[left]
index := right
for j := right; j > left; j-- {
if array[j] > pivot {
array[j], array[index]= array[index], array[j]
index--
}
}
array[left] = array[index]
array[index] = pivot
return index
}
// Divide
func quickSort(array []int, left int, right int) {
if left < right {
pivot := Partition(array, left, right)
quickSort(array, left, pivot-1)
quickSort(array, pivot+1, right)
}
}
// Calling function
func Quick_Sort(array []int, size int) {
quickSort(array, 0, size-1)
}
// Driver Function
func main() {
array:= []int{2, 4, 3, 1, 6, 8, 4}
Quick_Sort(array, len(array))
fmt.Println(array)
}
// Output
// [1 2 3 4 4 6 8]