0
0
DSA Cprogramming~10 mins

Quick Sort as Divide and Conquer in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Quick Sort as Divide and Conquer
Start with full array
Choose pivot element
Partition array into two parts
Elements < pivot
Recursively quick sort
Combine sorted parts + pivot
Sorted array
Quick Sort splits the array by a pivot, sorts parts separately, then combines them.
Execution Sample
DSA C
void quickSort(int arr[], int low, int high) {
  if (low < high) {
    int pi = partition(arr, low, high);
    quickSort(arr, low, pi - 1);
    quickSort(arr, pi + 1, high);
  }
}
This code sorts an array by dividing it around a pivot and sorting subarrays recursively.
Execution Table
StepOperationArray StatePivotPartition ActionResulting Subarrays
1Start quickSort on full array[10, 7, 8, 9, 1, 5]N/AN/AFull array: [10,7,8,9,1,5]
2Choose pivot[10, 7, 8, 9, 1, 5]5Prepare partition around 5Contains elements <5 (1) and >5
3Partition step[1, 7, 8, 9, 10, 5]5Swap 10↔1, then 5↔7Array after partition: [1,5,8,9,10,7]
4Pivot placed at index 1[1, 5, 8, 9, 10, 7]5Pivot 5 in correct placeLeft subarray: [1], Right subarray: [8,9,10,7]
5Recurse left subarray[1]N/ASingle element, no actionSorted left subarray: [1]
6Recurse right subarray[8, 9, 10, 7]7Choose pivot 7Partition around 7
7Partition step[1, 5, 7, 9, 10, 8]7Swap 8↔7 (pivot to pos 2)Full array after partition: [1,5,7,9,10,8]
8Pivot placed at index 2[1, 5, 7, 9, 10, 8]7Pivot 7 in correct placeLeft subarray: [], Right subarray: [9,10,8]
9Recurse left subarray[]N/AEmpty subarray, no actionSorted left subarray: []
10Recurse right subarray[9, 10, 8]8Choose pivot 8Partition around 8
11Partition step[1, 5, 7, 8, 10, 9]8Swap 9↔8 (pivot to pos 3)Full array after partition: [1,5,7,8,10,9]
12Pivot placed at index 3[1, 5, 7, 8, 10, 9]8Pivot 8 in correct placeLeft subarray: [], Right subarray: [10,9]
13Recurse left subarray[]N/AEmpty subarray, no actionSorted left subarray: []
14Recurse right subarray[10, 9]9Choose pivot 9Partition around 9
15Partition step & combine[1, 5, 7, 8, 9, 10]9Swap 10↔9 (pivot to pos 4)Final sorted array: [1,5,7,8,9,10]
16End[1, 5, 7, 8, 9, 10]N/ASorting completeSorted array returned
💡 Recursion ends when subarrays have zero or one element (base case).
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 6After Step 8After Step 10After Step 12Final
arr[10,7,8,9,1,5][1,5,8,9,10,7][1,5,8,9,10,7][1,5,8,9,10,7][1,5,7,9,10,8][1,5,7,9,10,8][1,5,7,8,10,9][1,5,7,8,9,10]
pivotN/A557788N/A
low0002233N/A
high5555555N/A
pi (pivot index)N/A11N/A2N/A3N/A
Key Moments - 3 Insights
Why does the pivot end up in the middle after partition?
Because partition swaps elements so that all smaller are left and larger are right, placing pivot at its correct sorted position (see Step 4 in execution_table).
Why do we stop recursion when subarray has one or zero elements?
Because such subarrays are already sorted, so no further action is needed (see Steps 5, 9, 13).
How does partition rearrange elements around the pivot?
Partition moves elements smaller than pivot to the left and larger to the right by swapping during traversal (see Steps 3, 7, 11, 15).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, where is the pivot placed in the array?
AAt index 0
BAt index 1
CAt index 5
DAt index 3
💡 Hint
Check the 'Pivot placed at index 1' row in execution_table.
At which step does the recursion stop for the left subarray containing only one element?
AStep 5
BStep 9
CStep 13
DStep 14
💡 Hint
Look for 'Single element, no action' for left subarray in execution_table.
If the pivot chosen was always the first element instead of last, how would the partition steps change?
APartition would not work and sorting would fail
BPivot would always be placed at the start
CPartition steps would swap different elements but overall logic remains same
DRecursion would stop earlier
💡 Hint
Partition logic depends on pivot position but still rearranges elements around it (see partition steps 3,7,11).
Concept Snapshot
Quick Sort divides array by pivot,
Partitions elements smaller and larger,
Recursively sorts subarrays,
Combines sorted parts,
Stops when subarray size ≤ 1.
Full Transcript
Quick Sort works by choosing a pivot element from the array and partitioning the other elements into two subarrays, according to whether they are less than or greater than the pivot. The subarrays are then sorted recursively. This divide and conquer approach continues until the subarrays have one or zero elements, which are inherently sorted. The partition step rearranges the array so that the pivot is placed in its correct sorted position, with smaller elements to the left and larger to the right. The process repeats on the left and right subarrays until the entire array is sorted.