11import Sort from '../Sort' ;
22
33export default class QuickSortInPlace extends Sort {
4- /* Sorting in place avoids unnecessary use of additional memory, but modifies input array.
4+ /** Sorting in place avoids unnecessary use of additional memory, but modifies input array.
55 *
66 * This process is difficult to describe, but much clearer with a visualization:
7- * http://www.algomation.com/algorithm/quick-sort-visualization
7+ * @see : http://www.algomation.com/algorithm/quick-sort-visualization
8+ *
9+ * @param {*[] } originalArray
10+ * @param {number } inputLowIndex
11+ * @param {number } inputHighIndex
12+ * @return {*[] }
813 */
9- sort ( originalArray , inputLow , inputHigh ) {
14+ sort ( originalArray , inputLowIndex , inputHighIndex ) {
1015 // Destructures array on initial passthrough, and then sorts in place.
11- const array = inputLow === undefined ? [ ...originalArray ] : originalArray ;
12- // Partition array segment and return index of last swap
13- const partition = ( l , h ) => {
14- const swap = ( left , right ) => {
15- const tempVariable = array [ left ] ;
16- array [ left ] = array [ right ] ;
17- array [ right ] = tempVariable ;
16+ const array = inputLowIndex === undefined ? [ ...originalArray ] : originalArray ;
17+
18+ /**
19+ * Partition array segment and return index of last swap
20+ *
21+ * @param {number } lowIndex
22+ * @param {number } highIndex
23+ * @return {number }
24+ */
25+ const partition = ( lowIndex , highIndex ) => {
26+ /**
27+ * @param {number } leftIndex
28+ * @param {number } rightIndex
29+ */
30+ const swap = ( leftIndex , rightIndex ) => {
31+ const tempVariable = array [ leftIndex ] ;
32+ array [ leftIndex ] = array [ rightIndex ] ;
33+ array [ rightIndex ] = tempVariable ;
1834 } ;
1935
20- const pivot = array [ h ] ;
36+ const pivot = array [ highIndex ] ;
2137 this . callbacks . visitingCallback ( array [ pivot ] ) ;
22- let firstRunner = l - 1 ;
2338
24- for ( let secondRunner = l ; secondRunner < h ; secondRunner += 1 ) {
39+ let firstRunner = lowIndex - 1 ;
40+ for ( let secondRunner = lowIndex ; secondRunner < highIndex ; secondRunner += 1 ) {
2541 if ( this . comparator . lessThan ( array [ secondRunner ] , pivot ) ) {
2642 firstRunner += 1 ;
2743 swap ( firstRunner , secondRunner ) ;
2844 }
2945 }
3046
3147 if ( this . comparator . lessThan ( pivot , array [ firstRunner + 1 ] ) ) {
32- swap ( firstRunner + 1 , h ) ;
48+ swap ( firstRunner + 1 , highIndex ) ;
3349 }
3450
3551 return firstRunner + 1 ;
@@ -40,20 +56,21 @@ export default class QuickSortInPlace extends Sort {
4056 * still have to set `high`'s default within the function as we
4157 * don't have access to `array.length - 1` when declaring paramaters
4258 */
43- const low = inputLow === undefined ? 0 : inputLow ;
44- const high = inputHigh === undefined ? array . length - 1 : inputHigh ;
59+ const lowIndex = inputLowIndex === undefined ? 0 : inputLowIndex ;
60+ const highIndex = inputHighIndex === undefined ? array . length - 1 : inputHighIndex ;
4561
4662 // Base case is when low and high converge
47- if ( low < high ) {
48- const partitionIndex = partition ( low , high ) ;
63+ if ( lowIndex < highIndex ) {
64+ const partitionIndex = partition ( lowIndex , highIndex ) ;
4965 /*
5066 * `partition()` swaps elements of the array based on their comparison to the `hi` parameter,
5167 * and then returns the index where swapping is no longer necessary, which can be best thought
5268 * of as the pivot used to split an array in a non-in-place quicksort
5369 */
54- this . sort ( array , low , partitionIndex - 1 ) ;
55- this . sort ( array , partitionIndex + 1 , high ) ;
70+ this . sort ( array , lowIndex , partitionIndex - 1 ) ;
71+ this . sort ( array , partitionIndex + 1 , highIndex ) ;
5672 }
73+
5774 return array ;
5875 }
5976}
0 commit comments