Skip to content

Commit 16562d2

Browse files
committed
Fix linting errors and clean up comments
1 parent 580c208 commit 16562d2

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

src/algorithms/sorting/quick-sort/QuickSort.js

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default class QuickSort extends Sort {
55
// Clone original array to prevent it from modification.
66
const array = [...originalArray];
77

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.
99
if (array.length <= 1) {
1010
return array;
1111
}
@@ -41,64 +41,55 @@ export default class QuickSort extends Sort {
4141
// Let's now join sorted left array with center array and with sorted right array.
4242
return leftArraySorted.concat(centerArray, rightArraySorted);
4343
}
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+
*/
5050
sortInPlace(array, low, high) {
51-
52-
// Helper function
51+
52+
// Partition array segment and return index of last swap
5353
const partition = (arr, l, h) => {
54-
55-
// Helper function
5654
const swap = (a, left, right) => {
5755
const tempVariable = a[left];
5856
a[left] = a[right];
5957
a[right] = tempVariable;
6058
}
61-
59+
6260
const pivot = arr[h];
6361
let firstRunner = l - 1;
64-
62+
6563
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+
7065
if (arr[secondRunner] < pivot) {
7166
firstRunner += 1;
7267
swap(arr, firstRunner, secondRunner);
7368
}
7469
}
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+
8071
if (arr[firstRunner + 1] > pivot) {
8172
swap(arr, firstRunner + 1, h);
8273
}
83-
74+
8475
return firstRunner + 1;
8576
};
86-
77+
8778
/*
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+
*/
9182
low = low === undefined ? 0 : low;
9283
high = high === undefined ? array.length - 1 : high;
93-
84+
9485
// Base case is when low and high converge
9586
if (low < high) {
9687
const partitionIndex = partition(array, low, high);
9788
/*
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+
*/
10293
this.sortInPlace(array, low, partitionIndex - 1);
10394
this.sortInPlace(array, partitionIndex + 1, high);
10495
}

0 commit comments

Comments
 (0)