15
15
Problem & Source of Explanation => https://www.cs.auckland.ac.nz/software/AlgAnim/qsort1a.html
16
16
*/
17
17
18
+
18
19
/**
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
23
24
*/
24
- const quickSort = ( inputList , low , high ) => {
25
+ // encapsulate the main function to to be the interface of our module
26
+ const quickSort = ( inputList ) => {
25
27
if ( ! Array . isArray ( inputList ) ) {
26
28
throw new TypeError ( 'Please input a valid list or array.' )
27
29
}
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
28
45
if ( low < high ) {
29
46
// get the partition index.
30
47
const pIndex = partition ( inputList , low , high )
31
48
// 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 )
34
51
}
35
52
return inputList
36
53
}
@@ -43,7 +60,9 @@ const quickSort = (inputList, low, high) => {
43
60
* @returns {number } `pIndex` pivot index value.
44
61
*/
45
62
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 )
47
66
let pIndex = low
48
67
for ( let index = low ; index <= high - 1 ; index ++ ) {
49
68
if ( partitionList [ index ] < pivot ) {
@@ -62,4 +81,22 @@ const partition = (partitionList, low, high) => {
62
81
return pIndex
63
82
}
64
83
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
+
65
102
export { quickSort }
0 commit comments