@@ -44,51 +44,50 @@ export default class QuickSort extends Sort {
44
44
45
45
/* Sorting in place avoids unnecessary use of additional memory, but modifies input array.
46
46
*
47
- * This process is difficult to describe, but much clearer with a visualization:
47
+ * This process is difficult to describe, but much clearer with a visualization:
48
48
* http://www.algomation.com/algorithm/quick-sort-visualization
49
49
*/
50
- sortInPlace ( array , low , high ) {
51
-
50
+ sortInPlace ( inputArray , inputLow , inputHigh ) {
51
+ const array = inputLow === undefined ? [ ... inputArray ] : inputArray ;
52
52
// Partition array segment and return index of last swap
53
- const partition = ( arr , l , h ) => {
54
- const swap = ( a , left , right ) => {
55
- const tempVariable = a [ left ] ;
56
- a [ left ] = a [ right ] ;
57
- a [ right ] = tempVariable ;
58
- }
53
+ const partition = ( l , h ) => {
54
+ const swap = ( left , right ) => {
55
+ const tempVariable = array [ left ] ;
56
+ array [ left ] = array [ right ] ;
57
+ array [ right ] = tempVariable ;
58
+ } ;
59
59
60
60
const pivot = arr [ h ] ;
61
61
let firstRunner = l - 1 ;
62
62
63
63
for ( let secondRunner = l ; secondRunner < h ; secondRunner += 1 ) {
64
-
65
- if ( arr [ secondRunner ] < pivot ) {
64
+ if ( array [ secondRunner ] < pivot ) {
66
65
firstRunner += 1 ;
67
- swap ( arr , firstRunner , secondRunner ) ;
66
+ swap ( firstRunner , secondRunner ) ;
68
67
}
69
68
}
70
69
71
- if ( arr [ firstRunner + 1 ] > pivot ) {
72
- swap ( arr , firstRunner + 1 , h ) ;
70
+ if ( array [ firstRunner + 1 ] > pivot ) {
71
+ swap ( firstRunner + 1 , h ) ;
73
72
}
74
73
75
74
return firstRunner + 1 ;
76
75
} ;
77
76
78
77
/*
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
78
+ * While we can use a default parameter to set `low` to 0, we would still have to set `high`'s
79
+ * default within the function as we don't have access to `array.length - 1` when declaring paramaters
81
80
*/
82
- low = low === undefined ? 0 : low ;
83
- high = high === undefined ? array . length - 1 : high ;
81
+ const low = inputLow === undefined ? 0 : inputLow ;
82
+ const high = inputHigh === undefined ? array . length - 1 : inputHigh ;
84
83
85
84
// Base case is when low and high converge
86
85
if ( low < high ) {
87
- const partitionIndex = partition ( array , low , high ) ;
86
+ const partitionIndex = partition ( low , high ) ;
88
87
/*
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
88
+ * `partition()` swaps elements of the array based on their comparison to the `hi` parameter,
89
+ * and then returns the index where swapping is no longer necessary, which can be best thought
90
+ * of as the pivot used to split an array in a non-in-place quicksort
92
91
*/
93
92
this . sortInPlace ( array , low , partitionIndex - 1 ) ;
94
93
this . sortInPlace ( array , partitionIndex + 1 , high ) ;
0 commit comments