|
1 |
| -/* |
2 |
| - Quicksort is the most popular sorting algorithm and there have |
3 |
| - lots of different implementations but the "recursive" or "Partition in place" |
4 |
| - is one of the most efficient implementations below we have discussed how to |
5 |
| - implement it. |
6 |
| -
|
7 |
| - Partition in place => "in place" Partition in place indicates that we |
8 |
| - do not need any other space to store the auxiliary array and the term |
9 |
| - "partition" denotes that we split the list into two parts one is less |
10 |
| - than the pivot and the other is greater than the pivot and repeats this |
11 |
| - process recursively and breaks the problem into sub-problems and makes |
12 |
| - it singular so that the behavior or "divide and conquer" get involved |
13 |
| - too. |
14 |
| -
|
15 |
| - Problem & Source of Explanation => https://www.cs.auckland.ac.nz/software/AlgAnim/qsort1a.html |
16 |
| -*/ |
17 |
| - |
18 | 1 | /**
|
19 |
| - * Partition in place QuickSort. |
20 |
| - * @param {number[]} inputList list of values. |
21 |
| - * @param {number} low lower index for partition. |
22 |
| - * @param {number} high higher index for partition. |
| 2 | + * @function quickSort |
| 3 | + * @description Quick sort is a comparison sorting algorithm that uses a divide and conquer strategy. |
| 4 | + * This version optimizes space complexity by sorting the array in place. |
| 5 | + * @param {Integer[]} items - Array of integers to be sorted. |
| 6 | + * @param {number} left - The starting index (defaults to 0). |
| 7 | + * @param {number} right - The ending index (defaults to array length - 1). |
| 8 | + * @return {Integer[]} - Sorted array. |
| 9 | + * @throws Will throw an error if the input is not an array. |
| 10 | + * @see [QuickSort](https://en.wikipedia.org/wiki/Quicksort) |
23 | 11 | */
|
24 |
| -const quickSort = (inputList, low, high) => { |
25 |
| - if (!Array.isArray(inputList)) { |
26 |
| - throw new TypeError('Please input a valid list or array.') |
| 12 | +function quickSort(items, left = 0, right = items.length - 1) { |
| 13 | + if (!Array.isArray(items)) { |
| 14 | + throw new Error('Please input a valid list or array.'); |
27 | 15 | }
|
28 |
| - if (low < high) { |
29 |
| - // get the partition index. |
30 |
| - const pIndex = partition(inputList, low, high) |
31 |
| - // recursively call the quickSort method again. |
32 |
| - quickSort(inputList, low, pIndex - 1) |
33 |
| - quickSort(inputList, pIndex + 1, high) |
| 16 | + |
| 17 | + if (left < right) { |
| 18 | + let pivotIndex = partition(items, left, right); |
| 19 | + quickSort(items, left, pivotIndex - 1); |
| 20 | + quickSort(items, pivotIndex + 1, right); |
34 | 21 | }
|
35 |
| - return inputList |
| 22 | + |
| 23 | + return items; |
36 | 24 | }
|
37 | 25 |
|
38 | 26 | /**
|
39 |
| - * Partition In Place method. |
40 |
| - * @param {number[]} partitionList list for partitioning. |
41 |
| - * @param {number} low lower index for partition. |
42 |
| - * @param {number} high higher index for partition. |
43 |
| - * @returns {number} `pIndex` pivot index value. |
| 27 | + * @function partition |
| 28 | + * @description This function partitions the array using the last element as the pivot. |
| 29 | + * It ensures that all elements smaller than the pivot are on the left side, |
| 30 | + * and all greater elements are on the right side. |
| 31 | + * @param {Integer[]} items - Array of integers to partition. |
| 32 | + * @param {number} left - The starting index for partitioning. |
| 33 | + * @param {number} right - The ending index for partitioning. |
| 34 | + * @return {number} - The index of the pivot element after partitioning. |
44 | 35 | */
|
45 |
| -const partition = (partitionList, low, high) => { |
46 |
| - const pivot = partitionList[high] |
47 |
| - let pIndex = low |
48 |
| - for (let index = low; index <= high - 1; index++) { |
49 |
| - if (partitionList[index] < pivot) { |
50 |
| - // swap variables using array destructuring |
51 |
| - ;[partitionList[index], partitionList[pIndex]] = [ |
52 |
| - partitionList[pIndex], |
53 |
| - partitionList[index] |
54 |
| - ] |
55 |
| - pIndex += 1 |
| 36 | +function partition(items, left, right) { |
| 37 | + const pivot = items[right]; |
| 38 | + let i = left - 1; |
| 39 | + |
| 40 | + for (let j = left; j < right; j++) { |
| 41 | + if (items[j] <= pivot) { |
| 42 | + i++; |
| 43 | + [items[i], items[j]] = [items[j], items[i]]; |
56 | 44 | }
|
57 | 45 | }
|
58 |
| - ;[partitionList[pIndex], partitionList[high]] = [ |
59 |
| - partitionList[high], |
60 |
| - partitionList[pIndex] |
61 |
| - ] |
62 |
| - return pIndex |
| 46 | + |
| 47 | + [items[i + 1], items[right]] = [items[right], items[i + 1]]; |
| 48 | + return i + 1; |
63 | 49 | }
|
64 | 50 |
|
65 |
| -export { quickSort } |
| 51 | +export { quickSort }; |
0 commit comments