0
0
DSA Goprogramming~10 mins

Quick Sort Algorithm in DSA Go - Execution Trace

Choose your learning style9 modes available
Concept Flow - Quick Sort Algorithm
Choose pivot element
Partition array around pivot
Elements < pivot to left
Elements > pivot to right
Recursively quicksort left part
Recursively quicksort right part
Combine sorted parts
Quick Sort picks a pivot, partitions the array around it, then sorts left and right parts recursively.
Execution Sample
DSA Go
func quickSort(arr []int, low, high int) {
  if low < high {
    p := partition(arr, low, high)
    quickSort(arr, low, p-1)
    quickSort(arr, p+1, high)
  }
}
This code sorts an array by partitioning around a pivot and recursively sorting subarrays.
Execution Table
StepOperationPivotArray StatePartition IndicesAction
1Choose pivot7[7, 2, 1, 6, 8, 5, 3, 4]low=0, high=7Pivot is last element 7
2Partition start7[7, 2, 1, 6, 8, 5, 3, 4]i= -1, j=0Compare arr[j]=7 with pivot=7
3Compare7[7, 2, 1, 6, 8, 5, 3, 4]i= -1, j=07 <= 7, increment i to 0, swap arr[i] and arr[j]
4Swap7[7, 2, 1, 6, 8, 5, 3, 4]i=0, j=0Swap arr[0] and arr[0] (no change)
5Compare7[7, 2, 1, 6, 8, 5, 3, 4]i=0, j=12 <= 7, increment i to 1, swap arr[i] and arr[j]
6Swap7[7, 2, 1, 6, 8, 5, 3, 4]i=1, j=1Swap arr[1] and arr[1] (no change)
7Compare7[7, 2, 1, 6, 8, 5, 3, 4]i=1, j=21 <= 7, increment i to 2, swap arr[i] and arr[j]
8Swap7[7, 2, 1, 6, 8, 5, 3, 4]i=2, j=2Swap arr[2] and arr[2] (no change)
9Compare7[7, 2, 1, 6, 8, 5, 3, 4]i=2, j=36 <= 7, increment i to 3, swap arr[i] and arr[j]
10Swap7[7, 2, 1, 6, 8, 5, 3, 4]i=3, j=3Swap arr[3] and arr[3] (no change)
11Compare7[7, 2, 1, 6, 8, 5, 3, 4]i=3, j=48 > 7, no increment, no swap
12Compare7[7, 2, 1, 6, 8, 5, 3, 4]i=3, j=55 <= 7, increment i to 4, swap arr[i] and arr[j]
13Swap7[7, 2, 1, 6, 8, 5, 3, 4]i=4, j=5Swap arr[4] and arr[5] → [7, 2, 1, 6, 5, 8, 3, 4]
14Compare7[7, 2, 1, 6, 5, 8, 3, 4]i=4, j=63 <= 7, increment i to 5, swap arr[i] and arr[j]
15Swap7[7, 2, 1, 6, 5, 8, 3, 4]i=5, j=6Swap arr[5] and arr[6] → [7, 2, 1, 6, 5, 3, 8, 4]
16Compare7[7, 2, 1, 6, 5, 3, 8, 4]i=5, j=74 <= 7, increment i to 6, swap arr[i] and arr[j]
17Swap7[7, 2, 1, 6, 5, 3, 8, 4]i=6, j=7Swap arr[6] and arr[7] → [7, 2, 1, 6, 5, 3, 4, 8]
18Partition end7[7, 2, 1, 6, 5, 3, 4, 8]i=6Swap pivot arr[7] and arr[i+1] arr[7] → [7, 2, 1, 6, 5, 3, 4, 8] becomes [7, 2, 1, 6, 5, 3, 4, 8]
19Pivot placed7[7, 2, 1, 6, 5, 3, 4, 8]pivot index=7Pivot 7 is now at index 7
20Recurse left-[7, 2, 1, 6, 5, 3, 4, 8]low=0, high=6QuickSort left subarray
21Choose pivot4[7, 2, 1, 6, 5, 3, 4, 8]low=0, high=6Pivot is last element 4
22Partition start4[7, 2, 1, 6, 5, 3, 4, 8]i=-1, j=0Compare arr[j]=7 with pivot=4
23Compare4[7, 2, 1, 6, 5, 3, 4, 8]i=-1, j=07 > 4, no increment, no swap
24Compare4[7, 2, 1, 6, 5, 3, 4, 8]i=-1, j=12 <= 4, increment i to 0, swap arr[i] and arr[j]
25Swap4[7, 2, 1, 6, 5, 3, 4, 8]i=0, j=1Swap arr[0] and arr[1] → [2, 7, 1, 6, 5, 3, 4, 8]
26Compare4[2, 7, 1, 6, 5, 3, 4, 8]i=0, j=21 <= 4, increment i to 1, swap arr[i] and arr[j]
27Swap4[2, 7, 1, 6, 5, 3, 4, 8]i=1, j=2Swap arr[1] and arr[2] → [2, 1, 7, 6, 5, 3, 4, 8]
28Compare4[2, 1, 7, 6, 5, 3, 4, 8]i=1, j=36 > 4, no increment, no swap
29Compare4[2, 1, 7, 6, 5, 3, 4, 8]i=1, j=45 > 4, no increment, no swap
30Compare4[2, 1, 7, 6, 5, 3, 4, 8]i=1, j=53 <= 4, increment i to 2, swap arr[i] and arr[j]
31Swap4[2, 1, 7, 6, 5, 3, 4, 8]i=2, j=5Swap arr[2] and arr[5] → [2, 1, 3, 6, 5, 7, 4, 8]
32Partition end4[2, 1, 3, 6, 5, 7, 4, 8]i=2Swap pivot arr[6] and arr[i+1] arr[3] → [2, 1, 3, 4, 5, 7, 6, 8]
33Pivot placed4[2, 1, 3, 4, 5, 7, 6, 8]pivot index=3Pivot 4 is now at index 3
34Recurse left-[2, 1, 3, 4, 5, 7, 6, 8]low=0, high=2QuickSort left subarray
35Choose pivot3[2, 1, 3, 4, 5, 7, 6, 8]low=0, high=2Pivot is last element 3
36Partition start3[2, 1, 3, 4, 5, 7, 6, 8]i=-1, j=0Compare arr[j]=2 with pivot=3
37Compare3[2, 1, 3, 4, 5, 7, 6, 8]i=-1, j=02 <= 3, increment i to 0, swap arr[i] and arr[j]
38Swap3[2, 1, 3, 4, 5, 7, 6, 8]i=0, j=0Swap arr[0] and arr[0] (no change)
39Compare3[2, 1, 3, 4, 5, 7, 6, 8]i=0, j=11 <= 3, increment i to 1, swap arr[i] and arr[j]
40Swap3[2, 1, 3, 4, 5, 7, 6, 8]i=1, j=1Swap arr[1] and arr[1] (no change)
41Compare3[2, 1, 3, 4, 5, 7, 6, 8]i=1, j=23 <= 3, increment i to 2, swap arr[i] and arr[j]
42Swap3[2, 1, 3, 4, 5, 7, 6, 8]i=2, j=2Swap arr[2] and arr[2] (no change)
43Partition end3[2, 1, 3, 4, 5, 7, 6, 8]i=2Swap pivot arr[2] and arr[i+1] arr[3] (no change)
44Pivot placed3[2, 1, 3, 4, 5, 7, 6, 8]pivot index=2Pivot 3 is now at index 2
45Recurse left-[2, 1, 3, 4, 5, 7, 6, 8]low=0, high=1QuickSort left subarray
46Choose pivot1[2, 1, 3, 4, 5, 7, 6, 8]low=0, high=1Pivot is last element 1
47Partition start1[2, 1, 3, 4, 5, 7, 6, 8]i=-1, j=0Compare arr[j]=2 with pivot=1
48Compare1[2, 1, 3, 4, 5, 7, 6, 8]i=-1, j=02 > 1, no increment, no swap
49Compare1[2, 1, 3, 4, 5, 7, 6, 8]i=-1, j=11 <= 1, increment i to 0, swap arr[i] and arr[j]
50Swap1[2, 1, 3, 4, 5, 7, 6, 8]i=0, j=1Swap arr[0] and arr[1] → [1, 2, 3, 4, 5, 7, 6, 8]
51Partition end1[1, 2, 3, 4, 5, 7, 6, 8]i=0Swap pivot arr[1] and arr[i+1] arr[1] (no change)
52Pivot placed1[1, 2, 3, 4, 5, 7, 6, 8]pivot index=1Pivot 1 is now at index 1
53Recurse left-[1, 2, 3, 4, 5, 7, 6, 8]low=0, high=0Base case reached, no action
54Recurse right-[1, 2, 3, 4, 5, 7, 6, 8]low=2, high=1Base case reached, no action
55Recurse right-[1, 2, 3, 4, 5, 7, 6, 8]low=4, high=6QuickSort right subarray
56Choose pivot6[1, 2, 3, 4, 5, 7, 6, 8]low=4, high=6Pivot is last element 6
57Partition start6[1, 2, 3, 4, 5, 7, 6, 8]i=3, j=4Compare arr[j]=5 with pivot=6
58Compare6[1, 2, 3, 4, 5, 7, 6, 8]i=3, j=45 <= 6, increment i to 4, swap arr[i] and arr[j]
59Swap6[1, 2, 3, 4, 5, 7, 6, 8]i=4, j=4Swap arr[4] and arr[4] (no change)
60Compare6[1, 2, 3, 4, 5, 7, 6, 8]i=4, j=57 > 6, no increment, no swap
61Compare6[1, 2, 3, 4, 5, 7, 6, 8]i=4, j=66 <= 6, increment i to 5, swap arr[i] and arr[j]
62Swap6[1, 2, 3, 4, 5, 7, 6, 8]i=5, j=6Swap arr[5] and arr[6] → [1, 2, 3, 4, 5, 6, 7, 8]
63Partition end6[1, 2, 3, 4, 5, 6, 7, 8]i=5Swap pivot arr[6] and arr[i+1] arr[6] (no change)
64Pivot placed6[1, 2, 3, 4, 5, 6, 7, 8]pivot index=6Pivot 6 is now at index 6
65Recurse left-[1, 2, 3, 4, 5, 6, 7, 8]low=4, high=5QuickSort left subarray
66Choose pivot5[1, 2, 3, 4, 5, 6, 7, 8]low=4, high=5Pivot is last element 5
67Partition start5[1, 2, 3, 4, 5, 6, 7, 8]i=3, j=4Compare arr[j]=5 with pivot=5
68Compare5[1, 2, 3, 4, 5, 6, 7, 8]i=3, j=45 <= 5, increment i to 4, swap arr[i] and arr[j]
69Swap5[1, 2, 3, 4, 5, 6, 7, 8]i=4, j=4Swap arr[4] and arr[4] (no change)
70Partition end5[1, 2, 3, 4, 5, 6, 7, 8]i=4Swap pivot arr[5] and arr[i+1] arr[5] (no change)
71Pivot placed5[1, 2, 3, 4, 5, 6, 7, 8]pivot index=5Pivot 5 is now at index 5
72Recurse left-[1, 2, 3, 4, 5, 6, 7, 8]low=4, high=4Base case reached, no action
73Recurse right-[1, 2, 3, 4, 5, 6, 7, 8]low=6, high=5Base case reached, no action
74Recurse right-[1, 2, 3, 4, 5, 6, 7, 8]low=7, high=6Base case reached, no action
75Recurse right-[1, 2, 3, 4, 5, 6, 7, 8]low=8, high=7Base case reached, no action
💡 All subarrays sorted, recursion ends when low >= high
Variable Tracker
VariableStartAfter Step 18After Step 33After Step 44After Step 50After Step 62Final
arr[7, 2, 1, 6, 8, 5, 3, 4][7, 2, 1, 6, 5, 3, 4, 8][2, 1, 3, 4, 5, 7, 6, 8][2, 1, 3, 4, 5, 7, 6, 8][1, 2, 3, 4, 5, 7, 6, 8][1, 2, 3, 4, 5, 6, 7, 8][1, 2, 3, 4, 5, 6, 7, 8]
pivot774316-
i-162205-
pivot index-73216-
Key Moments - 3 Insights
Why do we swap elements when arr[j] <= pivot during partition?
Swapping moves smaller elements to the left side of the pivot, ensuring correct partitioning as shown in steps 3-17.
Why is the pivot chosen as the last element in this example?
Choosing the last element as pivot is a common simple strategy; it simplifies partition logic as seen in step 1 and repeated in recursive calls.
When does the recursion stop?
Recursion stops when the subarray has zero or one element (low >= high), as shown in steps 53, 54, 72, 73, 74, 75.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 13, what is the array state after the swap?
A[7, 2, 1, 6, 8, 5, 3, 4]
B[7, 2, 1, 6, 5, 8, 3, 4]
C[7, 2, 1, 6, 3, 8, 5, 4]
D[7, 2, 1, 6, 5, 3, 8, 4]
💡 Hint
Check the 'Array State' column at step 13 in execution_table.
At which step is the pivot 4 placed in its final position?
AStep 33
BStep 44
CStep 18
DStep 50
💡 Hint
Look for 'Pivot placed' operation with pivot 4 in execution_table.
If the pivot was chosen as the first element instead of the last, how would the partition steps change?
ANo change in partition steps
BPivot would be swapped with last element before partition
CPartition would start from the first element and compare differently
DRecursion would stop earlier
💡 Hint
Consider how pivot choice affects partition start and comparisons in execution_table.
Concept Snapshot
Quick Sort Algorithm:
- Choose a pivot (commonly last element)
- Partition array: elements <= pivot left, > pivot right
- Recursively sort left and right subarrays
- Stops when subarray size <= 1
- Efficient average O(n log n) sorting
Full Transcript
Quick Sort works by picking a pivot element, usually the last in the current array segment. It then rearranges the array so that all elements less than or equal to the pivot come before it, and all greater elements come after. This is called partitioning. After partitioning, the pivot is in its correct sorted position. The algorithm then recursively applies the same process to the left and right parts of the array around the pivot. The recursion stops when the subarray has zero or one element, which means it is already sorted. The execution table shows each comparison and swap during partitioning, how the pivot moves to its final place, and how recursive calls sort smaller parts. The variable tracker shows how the array and pointers change after key steps. This visual step-by-step helps understand how Quick Sort efficiently sorts the array.