Skip to content

Commit 99eac6f

Browse files
committed
Change implementation to address lint errors
1 parent 16562d2 commit 99eac6f

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,51 +44,50 @@ export default class QuickSort extends Sort {
4444

4545
/* Sorting in place avoids unnecessary use of additional memory, but modifies input array.
4646
*
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:
4848
* http://www.algomation.com/algorithm/quick-sort-visualization
4949
*/
50-
sortInPlace(array, low, high) {
51-
50+
sortInPlace(inputArray, inputLow, inputHigh) {
51+
const array = inputLow === undefined ? [...inputArray] : inputArray;
5252
// 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+
};
5959

6060
const pivot = arr[h];
6161
let firstRunner = l - 1;
6262

6363
for (let secondRunner = l; secondRunner < h; secondRunner += 1) {
64-
65-
if (arr[secondRunner] < pivot) {
64+
if (array[secondRunner] < pivot) {
6665
firstRunner += 1;
67-
swap(arr, firstRunner, secondRunner);
66+
swap(firstRunner, secondRunner);
6867
}
6968
}
7069

71-
if (arr[firstRunner + 1] > pivot) {
72-
swap(arr, firstRunner + 1, h);
70+
if (array[firstRunner + 1] > pivot) {
71+
swap(firstRunner + 1, h);
7372
}
7473

7574
return firstRunner + 1;
7675
};
7776

7877
/*
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
8180
*/
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;
8483

8584
// Base case is when low and high converge
8685
if (low < high) {
87-
const partitionIndex = partition(array, low, high);
86+
const partitionIndex = partition(low, high);
8887
/*
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
9291
*/
9392
this.sortInPlace(array, low, partitionIndex - 1);
9493
this.sortInPlace(array, partitionIndex + 1, high);

0 commit comments

Comments
 (0)