0
0
DSA Javascriptprogramming

Bubble Sort Algorithm in DSA Javascript

Choose your learning style9 modes available
Mental Model
Bubble sort compares pairs of neighbors and swaps them if they are in the wrong order, pushing the largest unsorted value to the end each pass.
Analogy: Imagine bubbles rising in water: the biggest bubble slowly moves up to the surface by swapping places with smaller bubbles below it.
Array: [5, 3, 8, 4, 2]
Indexes:  0   1   2   3   4
Dry Run Walkthrough
Input: list: [5, 3, 8, 4, 2]
Goal: Sort the list in ascending order using bubble sort
Step 1: Compare 5 and 3, swap because 5 > 3
[3, 5, 8, 4, 2]
Why: Swap to move larger number rightwards
Step 2: Compare 5 and 8, no swap because 5 < 8
[3, 5, 8, 4, 2]
Why: Numbers in correct order, no change
Step 3: Compare 8 and 4, swap because 8 > 4
[3, 5, 4, 8, 2]
Why: Move larger number 8 rightwards
Step 4: Compare 8 and 2, swap because 8 > 2
[3, 5, 4, 2, 8]
Why: Largest number 8 bubbles to the end
Step 5: Start second pass: compare 3 and 5, no swap
[3, 5, 4, 2, 8]
Why: First two numbers in order
Step 6: Compare 5 and 4, swap because 5 > 4
[3, 4, 5, 2, 8]
Why: Move 5 rightwards
Step 7: Compare 5 and 2, swap because 5 > 2
[3, 4, 2, 5, 8]
Why: Move 5 rightwards
Step 8: Start third pass: compare 3 and 4, no swap
[3, 4, 2, 5, 8]
Why: Numbers in order
Step 9: Compare 4 and 2, swap because 4 > 2
[3, 2, 4, 5, 8]
Why: Move 4 rightwards
Step 10: Start fourth pass: compare 3 and 2, swap because 3 > 2
[2, 3, 4, 5, 8]
Why: Move smaller number leftwards
Result:
Final sorted array: [2, 3, 4, 5, 8]
Annotated Code
DSA Javascript
class BubbleSort {
  static sort(arr) {
    let n = arr.length;
    for (let i = 0; i < n - 1; i++) {
      // Each pass pushes largest unsorted to end
      for (let j = 0; j < n - 1 - i; j++) {
        if (arr[j] > arr[j + 1]) {
          // Swap if left is bigger
          let temp = arr[j];
          arr[j] = arr[j + 1];
          arr[j + 1] = temp;
        }
      }
    }
    return arr;
  }
}

// Driver code
const input = [5, 3, 8, 4, 2];
const sorted = BubbleSort.sort(input);
console.log(sorted);
for (let i = 0; i < n - 1; i++) {
outer loop controls number of passes
for (let j = 0; j < n - 1 - i; j++) {
inner loop compares adjacent pairs, shrinking range each pass
if (arr[j] > arr[j + 1]) {
compare neighbors to decide if swap needed
let temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp;
swap to move larger number rightwards
OutputSuccess
[2, 3, 4, 5, 8]
Complexity Analysis
Time: O(n^2) because each element is compared multiple times in nested loops
Space: O(1) because sorting is done in place without extra storage
vs Alternative: Compared to more advanced sorts like quicksort (O(n log n)), bubble sort is slower but simpler to understand
Edge Cases
empty array []
returns empty array immediately without error
DSA Javascript
for (let i = 0; i < n - 1; i++) {
array with one element [7]
returns the same single-element array unchanged
DSA Javascript
for (let i = 0; i < n - 1; i++) {
array with all equal elements [2, 2, 2]
no swaps occur, returns same array
DSA Javascript
if (arr[j] > arr[j + 1]) {
When to Use This Pattern
When you see a problem asking to sort by repeatedly swapping neighbors until sorted, think of bubble sort because it pushes largest values to the end step-by-step.
Common Mistakes
Mistake: Not reducing inner loop range each pass, causing unnecessary comparisons
Fix: Use 'n - 1 - i' as inner loop limit to avoid checking already sorted tail
Mistake: Swapping incorrectly or forgetting to swap neighbors
Fix: Always swap arr[j] and arr[j+1] when arr[j] > arr[j+1]
Summary
Bubble sort repeatedly swaps adjacent elements to sort an array in ascending order.
Use bubble sort for small or nearly sorted lists when simplicity matters more than speed.
The key insight is that each pass moves the largest unsorted element to its correct position at the end.