Topic: Sorting · Elementary and divide and conquer
Follow the computed execution on the reference input. Advance one step at a time, inspect the changing values and compare your prediction with the final output.
Example input
[
6,
2,
8,
1,
4
]
Execution steps
Partition around pivot
Place pivot
Computed output
[
1,
2,
4,
6,
8
]
JavaScript reference implementation
const solve = (a,s)=>{
function q(l,r){
if(l>=r)return;
let p=a[r],i=l;
for(let j=l;
j<r;
j++){
if(a[j]<=p){
[a[i],a[j]]=[a[j],a[i]];
i++
}
s("Partition around pivot",{
Array:a,Pivot:[p]
},[i,j,r])
}
[a[i],a[r]]=[a[r],a[i]];
s("Place pivot",{
Array:a
},[i]);
q(l,i-1);
q(i+1,r)
}
q(0,a.length-1);
return a
};