Skip to content

Commit f53cf76

Browse files
committed
feat: add function to choose better pivot and interface for the module
1 parent 9010481 commit f53cf76

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

Sorts/QuickSortRecursive.js

+45-8
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,39 @@
1515
Problem & Source of Explanation => https://www.cs.auckland.ac.nz/software/AlgAnim/qsort1a.html
1616
*/
1717

18+
1819
/**
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.
20+
* Sorts the input list using the quicksort algorithm.
21+
*
22+
* @param {Array} inputList - the list to be sorted
23+
* @return {Array} the sorted list
2324
*/
24-
const quickSort = (inputList, low, high) => {
25+
// encapsulate the main function to to be the interface of our module
26+
const quickSort = (inputList) => {
2527
if (!Array.isArray(inputList)) {
2628
throw new TypeError('Please input a valid list or array.')
2729
}
30+
return quickSortHelper(inputList, 0, inputList.length - 1)
31+
}
32+
33+
34+
/**
35+
* Recursively sorts the input list using the quicksort algorithm.
36+
*
37+
* @param {number[]} inputList - the list to be sorted
38+
* @param {number} low - the lower index of the current partition
39+
* @param {number} high - the higher index of the current partition
40+
* @return {number[]} the sorted list
41+
*/
42+
43+
const quickSortHelper = (inputList, low, high) => {
44+
// base case
2845
if (low < high) {
2946
// get the partition index.
3047
const pIndex = partition(inputList, low, high)
3148
// recursively call the quickSort method again.
32-
quickSort(inputList, low, pIndex - 1)
33-
quickSort(inputList, pIndex + 1, high)
49+
quickSortHelper(inputList, low, pIndex - 1)
50+
quickSortHelper(inputList, pIndex + 1, high)
3451
}
3552
return inputList
3653
}
@@ -43,7 +60,9 @@ const quickSort = (inputList, low, high) => {
4360
* @returns {number} `pIndex` pivot index value.
4461
*/
4562
const partition = (partitionList, low, high) => {
46-
const pivot = partitionList[high]
63+
let mid =Math.floor((low + high) / 2)
64+
// get the median of three is good technique for partitioning to be sure that the 2 sub-arrays will be almost equal or near
65+
const pivot = medianOfThree(partitionList, low, mid, high)
4766
let pIndex = low
4867
for (let index = low; index <= high - 1; index++) {
4968
if (partitionList[index] < pivot) {
@@ -62,4 +81,22 @@ const partition = (partitionList, low, high) => {
6281
return pIndex
6382
}
6483

84+
85+
/**
86+
* Returns the median value of three elements in an array.
87+
*
88+
* @param {number[]} arr - the input array
89+
* @param {number} low - the index of the first element
90+
* @param {number} mid - the index of the second element
91+
* @param {number} high - the index of the third element
92+
* @return {number} the median value of the three elements
93+
*/
94+
95+
const medianOfThree = (partitionList, low,mid, high) => {
96+
const a = partitionList[low], b = partitionList[mid], c = partitionList[high];
97+
if ((a > b) !== (a > c)) return a;
98+
else if ((b > a) !== (b > c)) return b;
99+
else return c;
100+
}
101+
65102
export { quickSort }

0 commit comments

Comments
 (0)