So, I have a program that calls the function : doQuickSort that takes the array i want sorted. I used a quicksort algorithm and everything seems fine to me but when returning the Partitionindex at the bottom of the Partition function I exceed the stack calls. If anyone can help me that would be great.
const animations = [];
export const swap = (arr, current, next) =>{
animations.push(arr[current]);
animations.push(arr[next]);
var curVal = arr[current];
arr[current] = arr[next];
arr[next] = curVal;
}
export const doQuickSort = (array) => {
var run = 0;
if(run == 0){
QuickSort(array, 0, array.length-1);
run++;
}
function partition(array, start, end){
let pivot = array[end];
let partitionIndex = start;
for(let i = start; i < end; i++){
if(array[i] <= pivot){
swap(array, i, partitionIndex);
partitionIndex++;
}
}
swap(array, partitionIndex, end);
partitionIndex++;
return partitionIndex;
}
function QuickSort(array, start, end){
if(start < end){
let partitionIndex = partition(array, start, end);
QuickSort(array, start, partitionIndex-1);
QuickSort(array, partitionIndex+1, end);
}
}
return array;
}