15
15
Problem & Source of Explanation => https://www.cs.auckland.ac.nz/software/AlgAnim/qsort1a.html
16
16
*/
17
17
18
-
19
18
/**
20
19
* Sorts the input list using the quicksort algorithm.
21
20
*
@@ -27,10 +26,12 @@ const quickSort = (inputList) => {
27
26
if ( ! Array . isArray ( inputList ) ) {
28
27
throw new TypeError ( 'Please input a valid list or array.' )
29
28
}
29
+ if ( inputList . length <= 1 ) {
30
+ return inputList
31
+ }
30
32
return quickSortHelper ( inputList , 0 , inputList . length - 1 )
31
33
}
32
34
33
-
34
35
/**
35
36
* Recursively sorts the input list using the quicksort algorithm.
36
37
*
@@ -41,13 +42,10 @@ const quickSort = (inputList) => {
41
42
*/
42
43
43
44
const quickSortHelper = ( inputList , low , high ) => {
44
- // base case
45
45
if ( low < high ) {
46
- // get the partition index.
47
- const pIndex = partition ( inputList , low , high )
48
- // recursively call the quickSort method again.
49
- quickSortHelper ( inputList , low , pIndex - 1 )
50
- quickSortHelper ( inputList , pIndex + 1 , high )
46
+ const pIndex = partition ( inputList , low , high ) // Partition the array
47
+ quickSortHelper ( inputList , low , pIndex - 1 ) // Sort left subarray
48
+ quickSortHelper ( inputList , pIndex + 1 , high ) // Sort right subarray
51
49
}
52
50
return inputList
53
51
}
@@ -60,28 +58,37 @@ const quickSortHelper = (inputList, low, high) => {
60
58
* @returns {number } `pIndex` pivot index value.
61
59
*/
62
60
const partition = ( partitionList , low , 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 )
61
+ const mid = Math . floor ( ( low + high ) / 2 )
62
+ // get the median of three is good technique for partitioning to be sure that the 2 sub-arrays will be almost equal or nearly equal in size
63
+ const pivot = medianOfThree ( partitionList , low , mid , high ) // Find the pivot element
64
+
65
+ // Move pivot to the end
66
+ let pivotIndex = partitionList . indexOf ( pivot )
67
+ ; [ partitionList [ pivotIndex ] , partitionList [ high ] ] = [
68
+ partitionList [ high ] ,
69
+ partitionList [ pivotIndex ]
70
+ ]
71
+
66
72
let pIndex = low
67
- for ( let index = low ; index <= high - 1 ; index ++ ) {
68
- if ( partitionList [ index ] < pivot ) {
69
- // swap variables using array destructuring
70
- ; [ partitionList [ index ] , partitionList [ pIndex ] ] = [
73
+
74
+ // Perform the partitioning
75
+ for ( let i = low ; i < high ; i ++ ) {
76
+ if ( partitionList [ i ] < pivot ) {
77
+ ; [ partitionList [ i ] , partitionList [ pIndex ] ] = [
71
78
partitionList [ pIndex ] ,
72
- partitionList [ index ]
79
+ partitionList [ i ]
73
80
]
74
- pIndex += 1
81
+ pIndex ++
75
82
}
76
83
}
84
+ // Swap the pivot element back to its correct position
77
85
; [ partitionList [ pIndex ] , partitionList [ high ] ] = [
78
86
partitionList [ high ] ,
79
87
partitionList [ pIndex ]
80
88
]
81
- return pIndex
82
- }
83
-
84
89
90
+ return pIndex // Return the partition index
91
+ }
85
92
/**
86
93
* Returns the median value of three elements in an array.
87
94
*
@@ -92,11 +99,14 @@ const partition = (partitionList, low, high) => {
92
99
* @return {number } the median value of the three elements
93
100
*/
94
101
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 ;
102
+ const medianOfThree = ( partitionList , low , mid , high ) => {
103
+ const a = partitionList [ low ]
104
+ const b = partitionList [ mid ]
105
+ const c = partitionList [ high ]
106
+
107
+ if ( a > b !== a > c ) return a
108
+ else if ( b > a !== b > c ) return b
109
+ else return c
100
110
}
101
111
102
112
export { quickSort }
0 commit comments