@@ -5,7 +5,7 @@ export default class QuickSort extends Sort {
5
5
// Clone original array to prevent it from modification.
6
6
const array = [ ...originalArray ] ;
7
7
8
- // If array has less then or equal to one elements then it is already sorted.
8
+ // If array has less than or equal to one elements then it is already sorted.
9
9
if ( array . length <= 1 ) {
10
10
return array ;
11
11
}
@@ -41,64 +41,55 @@ export default class QuickSort extends Sort {
41
41
// Let's now join sorted left array with center array and with sorted right array.
42
42
return leftArraySorted . concat ( centerArray , rightArraySorted ) ;
43
43
}
44
-
45
- /*
46
- While not always appropriate for the job at hand, if you don't mind modifying the input array,
47
- sorting in place offers space benefits, and while it doesn't change the time comlexity, it is
48
- usually more performant
49
- */
44
+
45
+ /* Sorting in place avoids unnecessary use of additional memory, but modifies input array.
46
+ *
47
+ * This process is difficult to describe, but much clearer with a visualization:
48
+ * http://www.algomation.com/algorithm/quick-sort-visualization
49
+ */
50
50
sortInPlace ( array , low , high ) {
51
-
52
- // Helper function
51
+
52
+ // Partition array segment and return index of last swap
53
53
const partition = ( arr , l , h ) => {
54
-
55
- // Helper function
56
54
const swap = ( a , left , right ) => {
57
55
const tempVariable = a [ left ] ;
58
56
a [ left ] = a [ right ] ;
59
57
a [ right ] = tempVariable ;
60
58
}
61
-
59
+
62
60
const pivot = arr [ h ] ;
63
61
let firstRunner = l - 1 ;
64
-
62
+
65
63
for ( let secondRunner = l ; secondRunner < h ; secondRunner += 1 ) {
66
- /*
67
- If `secondRunner` - `firstRunner` is 1, then element at index `secondRunner` swaps with itself. Regardless, swapping
68
- elements increments firstRunner
69
- */
64
+
70
65
if ( arr [ secondRunner ] < pivot ) {
71
66
firstRunner += 1 ;
72
67
swap ( arr , firstRunner , secondRunner ) ;
73
68
}
74
69
}
75
-
76
- /*
77
- This process is difficult to describe, but much clearer with a visualization:
78
- http://www.algomation.com/algorithm/quick-sort-visualization
79
- */
70
+
80
71
if ( arr [ firstRunner + 1 ] > pivot ) {
81
72
swap ( arr , firstRunner + 1 , h ) ;
82
73
}
83
-
74
+
84
75
return firstRunner + 1 ;
85
76
} ;
86
-
77
+
87
78
/*
88
- While we can use a default parameter to set `low` to 0, we would still have to set `high`'s default within the function
89
- as we don't have access to `array.length - 1` when declaring paramaters
90
- */
79
+ * While we can use a default parameter to set `low` to 0, we would still have to set `high`'s default within the function
80
+ * as we don't have access to `array.length - 1` when declaring paramaters
81
+ */
91
82
low = low === undefined ? 0 : low ;
92
83
high = high === undefined ? array . length - 1 : high ;
93
-
84
+
94
85
// Base case is when low and high converge
95
86
if ( low < high ) {
96
87
const partitionIndex = partition ( array , low , high ) ;
97
88
/*
98
- `partition()` swaps elements of the array based on their comparison to the `hi` parameter, and then returns the index
99
- where swapping is no longer necessary, which can be best thought of as the pivot used to split an array in a
100
- non-in-place quicksort
101
- */
89
+ * `partition()` swaps elements of the array based on their comparison to the `hi` parameter, and then returns the index
90
+ * where swapping is no longer necessary, which can be best thought of as the pivot used to split an array in a
91
+ * non-in-place quicksort
92
+ */
102
93
this . sortInPlace ( array , low , partitionIndex - 1 ) ;
103
94
this . sortInPlace ( array , partitionIndex + 1 , high ) ;
104
95
}
0 commit comments