Algorithms - Implement Quick Sort, should not use the built-in `.sort()` method

My Solution keeps giving me this error condition.
quickSort should not use the built-in .sort() method

I read another post suggesting that I should remove all comments, but is seems the recursive call of quickSort is the issue.

I am wondering if this test was broken my mistake, As all the other tests are passing.

Your code so far

function quickSort(array) {
  // Only change code below this line
    let start = arguments[1] || 0
    let end = arguments[2] || array.length - 1
  
	  if (end <= start) return array

  	let pivot = array[end]
    let i = start - 1 

    for(let j = start; j <= end - 1; j++) {
        if(array[j] < pivot) {  
            i++
            let temp = array[i]
            array[i] = array[j]
            array[j] = temp
        }
    }
    
    i++
    let temp = array[i]
    array[i] = array[end]
    array[end] = temp
    pivot = i       
  
	  array = quickSort(array, start, pivot - 1)   
	  array = quickSort(array, pivot + 1, end)     
  
    return array
  // Only change code above this line
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Algorithms - Implement Quick Sort

Link to the challenge:

I suspect that your recursive approach is tripping up the check for the word sort.

edit: nope, one of the other solutions use recursion

edit again:

this line is triggering the false warning.

2 Likes

Did you try your function with other arrays or just the one in the test?

The test calls this quickSort([0, 1]); you can see what happens when you make that call.

1 Like

Thank you JeremyLT this was where the issue was.
The error message was a red herring.
Ternary operator to the rescue.

Thank you Lasjorg, this data along with the other post helped me to debug the edge case.

1 Like

Ideally, I would have defined my function like,

function quickSort(array, start = 0, end = array.length-1)

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.