-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathQuickSort.js
More file actions
46 lines (41 loc) · 1.56 KB
/
QuickSort.js
File metadata and controls
46 lines (41 loc) · 1.56 KB
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
/**
* @function quickSort
* @description Quick sort is a comparison sorting algorithm that uses a divide and conquer strategy.
* This version optimizes space complexity by sorting the array in place.
* @param {Integer[]} items - Array of integers to be sorted.
* @param {number} left - The starting index (defaults to 0).
* @param {number} right - The ending index (defaults to array length - 1).
* @return {Integer[]} - Sorted array.
* @see [QuickSort](https://en.wikipedia.org/wiki/Quicksort)
*/
function quickSort(items, left = 0, right = items.length - 1) {
if (left < right) {
let pivotIndex = partition(items, left, right)
quickSort(items, left, pivotIndex - 1)
quickSort(items, pivotIndex + 1, right)
}
return items
}
/**
* @function partition
* @description This function partitions the array using the last element as the pivot.
* It ensures that all elements smaller than the pivot are on the left side,
* and all greater elements are on the right side.
* @param {Integer[]} items - Array of integers to partition.
* @param {number} left - The starting index for partitioning.
* @param {number} right - The ending index for partitioning.
* @return {number} - The index of the pivot element after partitioning.
*/
function partition(items, left, right) {
const pivot = items[right]
let i = left - 1
for (let j = left; j < right; j++) {
if (items[j] <= pivot) {
i++
;[items[i], items[j]] = [items[j], items[i]]
}
}
;[items[i + 1], items[right]] = [items[right], items[i + 1]]
return i + 1
}
export { quickSort }