Workspace Quick Sort
0d
Back to questionsDSA Visualizers: 388 Coding Questions

Quick Sort visualizer

Learn Quick Sort with a step-by-step Sorting visualizer, JavaScript source code and a worked example. Practice elementary and divide and conquer.

INTERACTIVE EXECUTION STUDIO
LEARN BY DOING

Predict. Execute. Understand.

Follow the source code, inspect live state changes, and predict the next operation before revealing it.

1 / 10Execution step
Reference example · JavaScript
Inspect reference input
[
  6,
  2,
  8,
  1,
  4
]

Algorithm source

JavaScript reference implementation · highlighted trace statement
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
};

Execution canvas

Active Changed
STEP 1

Partition around pivot

Inspect the highlighted code and the state below. Click individual cells or nodes to see their values.

Array5 items
Pivot1 items
Keyboard: Space to play · Left / Right to stepExplore next problem

Worked example and algorithm walkthrough

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

  1. Partition around pivot
  2. 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
};

Continue learning

Practice related DSA questions in the same topic.