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)
| 6 | // - Time complexity O(nlog n) |
| 7 | // - Space complexity O(log n) |
| 8 | func 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. |
| 25 | func partition(randomList []int, leftIdx, rightIdx int) ([]int, int) { |