0
0
DSA Javascriptprogramming~10 mins

Quick Sort Algorithm in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Quick Sort Algorithm
Choose pivot element
Partition array around pivot
Left sub-array < pivot
Recursively quick sort left
Combine sorted parts
Done
Quick Sort picks a pivot, splits the array into smaller and bigger parts, sorts each part by repeating the process, then combines them.
Execution Sample
DSA Javascript
function quickSort(arr) {
  if (arr.length <= 1) return arr;
  const pivot = arr[arr.length - 1];
  const left = [];
  const right = [];
  for (let i = 0; i < arr.length - 1; i++) {
    if (arr[i] < pivot) left.push(arr[i]); else right.push(arr[i]);
  }
  return [...quickSort(left), pivot, ...quickSort(right)];
}
This code sorts an array by choosing the last element as pivot, splitting into left and right arrays, sorting them recursively, and combining.
Execution Table
StepOperationArray StatePivotLeft ArrayRight ArrayAction
1Start quickSort[8, 3, 7, 6, 2]2[][8, 3, 7, 6]Choose pivot 2
2Partition[8, 3, 7, 6, 2]2[][8, 3, 7, 6]8 > 2 → right, 3 > 2 → right, 7 > 2 → right, 6 > 2 → right
3Recurse left[]-[][]Left empty, return []
4Recurse right[8, 3, 7, 6]6[3][8, 7]Choose pivot 6, partition into left [3], right [8,7]
5Partition right[8, 3, 7, 6]6[3][8, 7]3 < 6 → left, 8 > 6 → right, 7 > 6 → right
6Recurse left of right[3]3[][]Single element, return [3]
7Recurse left of left of right[]-[][]Left empty, return []
8Recurse right of left of right[]-[][]Right empty, return []
9Combine left of right[3]3[][]Combine [] + 3 + [] → [3]
10Recurse right of right[8, 7]7[][8]Choose pivot 7, left empty, right [8]
11Recurse left of right of right[]-[][]Left empty, return []
12Recurse right of right of right[8]8[][]Single element, return [8]
13Combine right of right[7, 8]7[][8]Combine [] + 7 + [8] → [7,8]
14Combine right[3, 6, 7, 8]6[3][7, 8]Combine [3] + 6 + [7,8] → [3,6,7,8]
15Combine all[2, 3, 6, 7, 8]2[][3, 6, 7, 8]Combine [] + 2 + [3,6,7,8] → [2,3,6,7,8]
16End[2, 3, 6, 7, 8]---Array sorted
💡 Recursion ends when sub-array length is 0 or 1, fully sorted array returned
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 9After Step 13Final
arr[8, 3, 7, 6, 2][8, 3, 7, 6, 2][8, 3, 7, 6][3][3][7, 8][2, 3, 6, 7, 8]
pivot226337-
left[][][3][][][]-
right[][8, 3, 7, 6][8, 7][][][8]-
Key Moments - 3 Insights
Why do we choose the last element as the pivot?
Choosing the last element as pivot is a simple rule to split the array; see Step 1 and Step 4 in execution_table where pivot is last element.
What happens when the left or right sub-array is empty?
When left or right is empty, recursion returns immediately as in Step 3 and Step 7, preventing infinite loops.
How does the array get combined back after sorting sub-arrays?
After sorting left and right, the arrays are combined with pivot in the middle, shown in Steps 9, 13, and 14.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the pivot chosen?
A6
B2
C8
D3
💡 Hint
Check the 'Pivot' column at Step 4 in execution_table
At which step does the left sub-array become empty for the first time?
AStep 6
BStep 7
CStep 3
DStep 11
💡 Hint
Look at 'Left Array' column in execution_table rows for empty array
If the pivot was chosen as the first element instead of last, how would the partition step change?
ALeft and right arrays would be swapped
BPivot would be different, changing partition elements
CNo change in partitioning
DAlgorithm would fail
💡 Hint
Pivot choice affects which elements go left or right, see Steps 1 and 4
Concept Snapshot
Quick Sort Algorithm:
- Pick a pivot (commonly last element)
- Partition array into left (< pivot) and right (> pivot)
- Recursively sort left and right sub-arrays
- Combine sorted left + pivot + sorted right
- Stops when sub-array size ≤ 1
- Efficient average O(n log n) sorting
Full Transcript
Quick Sort works by choosing a pivot element, usually the last in the array. It then splits the array into two parts: elements less than the pivot go to the left, and elements greater go to the right. Each part is sorted by repeating the same process recursively. When the parts are small enough (empty or one element), they are returned as sorted. Finally, the sorted left part, the pivot, and the sorted right part are combined to form the fully sorted array. This process is efficient and widely used for sorting arrays.