Quicksort In-Place

  • + 0 comments

    Does anyone know why this code is gives me an "RangeError: Maximum call stack size exceeded"?

    I have tried the code on many other platforms and it works, but here I get that error...

    function processData(arr) {
      //Enter your code here
      const pivot = arr.length - 1;
      let i = -1;
      
      if(arr.length <= 1) return arr
    
      for (let j = 0; j < arr.length; j++) {
        
        if (arr[j] < arr[pivot]) {
          //we increment i
          i++;
    
          //we swamp elements
          const toSwamp = arr[i];
          arr[i] = arr[j];
          arr[j] = toSwamp;
         
        }
    
        if (j === pivot) {
          const toSwamp = arr[i + 1];
          arr[i + 1] = arr[pivot];
          arr[pivot] = toSwamp;
        
        }
      }
      
      console.log(arr)
      return processData(arr.slice(0, i+1)).concat(processData(arr.slice(i+1, pivot+1)))
    }