0
0
DSA C++programming~10 mins

Bubble Sort Algorithm in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Bubble Sort Algorithm
Start with unsorted array
Compare adjacent elements
If left > right?
YesSwap elements
Move to next pair
Reach end of array?
NoRepeat compare-swap
Yes
Array sorted, stop
Bubble Sort repeatedly compares pairs of adjacent elements and swaps them if they are in the wrong order, bubbling the largest unsorted element to the end each pass.
Execution Sample
DSA C++
int arr[] = {5, 3, 8, 1, 2};
int n = 5;
for (int i = 0; i < n-1; i++) {
  for (int j = 0; j < n-i-1; j++) {
    if (arr[j] > arr[j+1]) {
      std::swap(arr[j], arr[j+1]);
    }
  }
}
Sorts the array {5,3,8,1,2} using Bubble Sort by repeatedly swapping adjacent elements if out of order.
Execution Table
StepPassCompare IndicesCompare ValuesSwap?Array State After Step
110 and 15 and 3Yes[3, 5, 8, 1, 2]
211 and 25 and 8No[3, 5, 8, 1, 2]
312 and 38 and 1Yes[3, 5, 1, 8, 2]
413 and 48 and 2Yes[3, 5, 1, 2, 8]
520 and 13 and 5No[3, 5, 1, 2, 8]
621 and 25 and 1Yes[3, 1, 5, 2, 8]
722 and 35 and 2Yes[3, 1, 2, 5, 8]
830 and 13 and 1Yes[1, 3, 2, 5, 8]
931 and 23 and 2Yes[1, 2, 3, 5, 8]
1040 and 11 and 2No[1, 2, 3, 5, 8]
💡 No swaps needed in last pass, array is sorted.
Variable Tracker
VariableStartAfter Step 4After Step 7After Step 9Final
arr[5, 3, 8, 1, 2][3, 5, 1, 2, 8][3, 1, 2, 5, 8][1, 2, 3, 5, 8][1, 2, 3, 5, 8]
i (pass)00124
j (compare index)03210
Key Moments - 3 Insights
Why do we stop inner loop at n - i - 1 instead of n - 1?
Because after each pass i, the last i elements are already sorted and in place, so we don't need to compare them again. See execution_table rows where pass increases and inner loop range decreases.
Why do we swap only when left element is greater than right?
Swapping only when left > right ensures the larger elements move towards the end, sorting the array in ascending order. This is shown in execution_table rows where swaps happen only if arr[j] > arr[j+1].
What happens if the array is already sorted?
If the array is sorted, no swaps occur during a pass, so the algorithm can stop early. In this example, the last pass (pass 4) has no swaps, so sorting ends.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the array state after step 4?
A[3, 5, 1, 2, 8]
B[5, 3, 8, 1, 2]
C[3, 1, 2, 5, 8]
D[1, 2, 3, 5, 8]
💡 Hint
Check the 'Array State After Step' column in execution_table row with Step 4.
At which step does the first swap happen in pass 3?
AStep 6
BStep 8
CStep 5
DStep 9
💡 Hint
Look for pass 3 in execution_table and find the first 'Yes' in Swap? column.
If we change the array to already sorted [1, 2, 3, 5, 8], how would the swaps change?
ASwaps only in first pass
BSwaps in every pass
CNo swaps at all
DSwaps only in last pass
💡 Hint
Refer to key_moments about what happens if array is already sorted.
Concept Snapshot
Bubble Sort Algorithm:
- Repeatedly compare adjacent elements
- Swap if left > right
- Largest unsorted element moves to end each pass
- Inner loop runs n - i - 1 times per pass
- Stops early if no swaps in a pass
Full Transcript
Bubble Sort works by comparing pairs of adjacent elements in an array. If the left element is bigger than the right, they swap places. This process repeats for each pair in the array, pushing the largest unsorted element to the end. Each pass reduces the number of elements to check because the end elements become sorted. The algorithm stops when a pass completes with no swaps, meaning the array is sorted. The example array {5,3,8,1,2} is sorted step-by-step, showing swaps and array states after each comparison.