MCPcopy Create free account
hub / github.com/ardanlabs/gotraining / quickSort

Function quickSort

topics/go/algorithms/sorting/quick/quick.go:8–22  ·  view source on GitHub ↗

quickSort is an in-place sorting algorithm. It takes a random list of numbers, and uses the `recursive` process to divides it into partitions then sorts those. - Time complexity O(nlog n) - Space complexity O(log n)

(randomList []int, leftIdx, rightIdx int)

Source from the content-addressed store, hash-verified

6// - Time complexity O(nlog n)
7// - Space complexity O(log n)
8func quickSort(randomList []int, leftIdx, rightIdx int) []int {
9 switch {
10 case leftIdx > rightIdx:
11 return randomList
12
13 // Divides array into two partitions.
14 case leftIdx < rightIdx:
15 randomList, pivotIdx := partition(randomList, leftIdx, rightIdx)
16
17 quickSort(randomList, leftIdx, pivotIdx-1)
18 quickSort(randomList, pivotIdx+1, rightIdx)
19 }
20
21 return randomList
22}
23
24// partition it takes a portion of an array then sort it.
25func partition(randomList []int, leftIdx, rightIdx int) ([]int, int) {

Callers 2

TestQuickSortFunction · 0.85
BenchmarkQuickSortFunction · 0.85

Calls 1

partitionFunction · 0.85

Tested by 2

TestQuickSortFunction · 0.68
BenchmarkQuickSortFunction · 0.68