0
0
DSA Javascriptprogramming~10 mins

Bubble Sort Algorithm in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Bubble Sort Algorithm
Start with array
Compare adjacent elements
If left > right?
NoMove to next pair
|Yes
Swap elements
Reach end of array?
NoRepeat compare-swap
Yes
Reduce array length by 1
Is array length > 1?
YesRepeat passes
No
Sorted array
Bubble Sort repeatedly compares and swaps adjacent elements, pushing the largest unsorted element to the end each pass, until the array is sorted.
Execution Sample
DSA Javascript
const arr = [5, 3, 8, 1, 2];
for (let i = 0; i < arr.length - 1; i++) {
  let swapped = false;
  for (let j = 0; j < arr.length - 1 - i; j++) {
    if (arr[j] > arr[j + 1]) {
      [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      swapped = true;
    }
  }
  if (!swapped) {
    break;
  }
}
This code sorts the array by repeatedly swapping adjacent elements if they are in the wrong order.
Execution Table
StepOperationComparisonSwap?Array State
1Compare arr[0] and arr[1]5 > 3Yes[3, 5, 8, 1, 2]
2Compare arr[1] and arr[2]5 > 8No[3, 5, 8, 1, 2]
3Compare arr[2] and arr[3]8 > 1Yes[3, 5, 1, 8, 2]
4Compare arr[3] and arr[4]8 > 2Yes[3, 5, 1, 2, 8]
5Compare arr[0] and arr[1]3 > 5No[3, 5, 1, 2, 8]
6Compare arr[1] and arr[2]5 > 1Yes[3, 1, 5, 2, 8]
7Compare arr[2] and arr[3]5 > 2Yes[3, 1, 2, 5, 8]
8Compare arr[0] and arr[1]3 > 1Yes[1, 3, 2, 5, 8]
9Compare arr[1] and arr[2]3 > 2Yes[1, 2, 3, 5, 8]
10Compare arr[0] and arr[1]1 > 2No[1, 2, 3, 5, 8]
11No more comparisons, array sorted-No[1, 2, 3, 5, 8]
💡 All passes completed, no swaps needed, array is sorted
Variable Tracker
VariableStartAfter Step 1After Step 4After Step 7After Step 9Final
arr[5, 3, 8, 1, 2][3, 5, 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)000123
j (index)03321-
Key Moments - 3 Insights
Why do we reduce the inner loop length by 'i' each pass?
Because after each pass, the largest element settles at the end, so we don't need to compare it again. See execution_table rows 4 and 7 where the last elements are already sorted.
Why do we swap only when the left element is greater than the right?
Swapping only when left > right ensures the larger elements move right, sorting the array ascendingly. This is shown in execution_table rows 1, 3, 4 where swaps happen only if left > right.
What stops the algorithm from running forever?
The outer loop runs a fixed number of passes (array length - 1). Also, when no swaps happen in a pass, the array is sorted and the algorithm stops, as in execution_table row 11.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the array state after step 4?
A[5, 3, 8, 1, 2]
B[3, 5, 8, 1, 2]
C[3, 5, 1, 2, 8]
D[1, 2, 3, 5, 8]
💡 Hint
Check the 'Array State' column in execution_table row 4
At which step does the algorithm first swap the elements 3 and 1?
AStep 8
BStep 6
CStep 9
DStep 2
💡 Hint
Look for the comparison '3 > 1' with a swap in execution_table
If the array was already sorted, how would the execution_table change?
AAll comparisons would result in swaps
BNo swaps would occur, and the algorithm would stop early
CThe array state would change after every step
DThe algorithm would run infinitely
💡 Hint
Refer to the exit_note and step 11 where no swaps mean sorted array
Concept Snapshot
Bubble Sort Algorithm:
- Repeatedly compare adjacent elements
- Swap if left element is greater
- Largest unsorted element moves to end each pass
- Reduce inner loop length each pass
- Stop when no swaps or passes complete
Full Transcript
Bubble Sort works by comparing pairs of adjacent elements in an array. If the left element is bigger, they swap places. This pushes the largest unsorted element to the end of the array each pass. After each pass, the algorithm reduces the number of comparisons because the end elements are sorted. The process repeats until no swaps are needed, meaning the array is sorted. The execution table shows each comparison and swap step, tracking how the array changes. Key moments include understanding why the inner loop shrinks and why swaps only happen when the left element is bigger. The algorithm stops after enough passes or when the array is sorted early.