Understanding the process in quickSort

after some suggestions i got a solution, but i’m struggling to understand the process, the order of steps, etc. and it seems impossible to see it with some console logs as its recursive.

anyone got a quick description of the step by step process at play here? does the lesser recursion go first? it seems to import multiple pivots with one call to a singular pivot. i can’t even ask the question very well with this, lol.

``````  **Your code so far**
``````
``````function quickSort(array) {
if(array.length <= 1) { return array }
const lesser=[]
const greater=[]
const pivot = array[array.length-1]
for(let i=0;i<array.length-1;i++){
if(array[i]<pivot){lesser.push(array[i])}
else{greater.push(array[i])}
}
return [ ...quickSort(lesser), pivot, ...quickSort(greater)]
// FIRST RECURSIVE LESSER FINDS ORDER TO FIRST ~HALF...
//  ?THEN WHATS LEFT IN THE GREATER IS RECURSED INTO ORDER
}
console.log('result:', quickSort([1,4,2,8,345,123,43,32,5643,63,1]))
``````
``````  **Your browser information:**
``````

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

Challenge: Implement Quick Sort

`console.log` still can be used to get some insight at what is happening in what order. Take a look:

``````function quickSort(array) {
console.log('array', array)
if(array.length <= 1) { return array }
const lesser=[]
const greater=[]
const pivot = array[array.length-1]
for(let i=0;i<array.length-1;i++){
if(array[i]<pivot){lesser.push(array[i])}
else{greater.push(array[i])}
}
console.log('pivot', pivot);
console.log('lesser', lesser);
console.log('greater', greater);
console.log()
return [ ...quickSort(lesser), pivot, ...quickSort(greater)]
}
``````

now i forget what i had done but actually that is useful, thankyou.

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