I have been trying to implement a quickSort function and have everything working. But there is one peculiarity that I cannot wrap my head around or understand why.
In this first block of code, you will see that I have declared some default param values for the quickSort()
function:
function swap(arr, firstIndex, secondIndex) {
let temp = arr[firstIndex];
arr[firstIndex] = arr[secondIndex];
arr[secondIndex] = temp;
}
function pivot(arr, start = 0, end = arr.length - 1) {
// We are assuming that the pivot is always the first element
let pivot = arr[start];
let swapIndex = start;
for (let i = start + 1; i <= end; i++) {
if (pivot > arr[i]) {
swapIndex++;
swap(arr, swapIndex, i);
}
}
// Swap the starting element with the pivot index
swap(arr, start, swapIndex);
return swapIndex;
}
function quickSort(arr, left = 0, right = arr.length - 1) {
if (left < right) {
let pivotIndex = pivot(arr, left, right);
// left
quickSort(arr, left, pivotIndex - 1);
// right
quickSort(arr, pivotIndex + 1, right);
}
return arr;
}
In this example, it works fine as expected. However, if I were to remove the ES2015 default param values from quickSort()
and, instead, create the defaults inside of the function, like this:
function quickSort(arr, left, right) {
left = left || 0;
right = right || arr.length -1;
if (left < right) {
let pivotIndex = pivot(arr, left, right);
// left
quickSort(arr, left, pivotIndex - 1);
// right
quickSort(arr, pivotIndex + 1, right);
}
return arr;
}
I get an infinite loop/Stack Overflow issue and I cannot understand why. From what I can tell, the issue is caused by the third param - right
- rather than the left param, as the code works fine if I call the left param using the pre-es2015 method, whilst leaving the right
param with the ES2015 param default method.
All in all, I have my code working, so that's fine - I just want to try and better understand why this would cause an issue, as I've never encountered such a problem before.
Thanks!