-
Notifications
You must be signed in to change notification settings - Fork 11
/
quicksort.go
40 lines (35 loc) · 1 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
// quicksort.go
// description: Implementation of in-place quicksort algorithm
// details:
// A simple in-place quicksort algorithm implementation. [Wikipedia](https://en.wikipedia.org/wiki/Quicksort)
// author(s) [Taj](https://github.com/tjgurwara99)
// see sort_test.go for a test implementation, test function TestQuickSort.
package sort
func partition(arr []int, low, high int) int {
index := low - 1
pivotElement := arr[high]
for i := low; i < high; i++ {
if arr[i] <= pivotElement {
index += 1
arr[index], arr[i] = arr[i], arr[index]
}
}
arr[index+1], arr[high] = arr[high], arr[index+1]
return index + 1
}
// QuickSortRange Sorts the specified range within the array
func QuickSortRange(arr []int, low, high int) {
if len(arr) <= 1 {
return
}
if low < high {
pivot := partition(arr, low, high)
QuickSortRange(arr, low, pivot-1)
QuickSortRange(arr, pivot+1, high)
}
}
// QuickSort Sorts the entire array
func QuickSort(arr []int) []int {
QuickSortRange(arr, 0, len(arr)-1)
return arr
}