Typorithm · easy

Bubble Sort

▷ Interactive algorithm labsortingbubble-sortnested-loopsquadratic-time
Saved on this device
Full lesson · 35 learning steps Open any exercise for focused practice
1

Step 1 of 35 · intro card

Learn Bubble Sort

0%

Sorting Algorithms

Bubble Sort repeatedly compares neighbors and swaps them when they are out of order.

Start at the left side of the array and compare two neighboring values.

If the left value is larger than the right value, swap them. If not, leave them in place and move one position to the right.

After one full pass, the largest unsorted value has moved to the end. The next pass can ignore that sorted tail.

Bubble Sort is slow for large arrays, but it is useful for learning nested loops, comparisons, swaps, and time complexity because every move is visible.

Preparing algorithm lab…
01

Compare neighboring elements.

02

Swap the pair when the left value is greater.

03

Ignore the sorted tail after each pass.

04

Average and worst time are O(n^2).

function bubbleSort(arr) {
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
      if (arr[j] > arr[j + 1]) {
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
      }
    }
  }

  return arr;
}

Go beyond the happy path

What the loop guarantees

The pass invariant

After pass i, the i largest values occupy their final positions at the right. Inside a pass, the largest value seen so far moves one neighbor to the right. That is why the inner loop stops before the sorted tail.

Stability and mutation

The strict > comparison leaves equal values in their original relative order. Changing it to >= loses that stability. Swapping writes into the input array; callers that need the original should copy it before calling bubbleSort.

Cost of this implementation

The inner loop makes n(n−1)/2 comparisons even on sorted input: this basic version takes O(n²) time in all cases. It uses O(1) auxiliary space. An early-exit flag can reduce the best case to O(n), but that is a different implementation.

Cases worth checking

Empty and single-value arrays need no comparison. Reversed arrays trigger every possible swap. Duplicates must remain stable; negative values still use the same numeric comparison. The trace above lets you compare mixed, reversed and duplicate inputs.

bubbleSort([])             // []
bubbleSort([7])            // [7]
bubbleSort([3, 1, 3, 2])   // [1, 2, 3, 3]
bubbleSort([-1, 4, -3])    // [-3, -1, 4]

Reflection: which boundary would read beyond the array, and why does one successful swap not prove that the whole array is sorted?

Read the implementation

Connect each part of the code to the concept before you type.

1. Function declaration

Define a reusable function that receives the array to sort.

function bubbleSort(arr)

2. Outer loop

The outer loop controls how many full passes we make.

for (let i = 0; i < arr.length; i++)

3. Inner loop

The inner loop walks across neighboring pairs.

for (let j = 0; j < arr.length - i - 1; j++)

4. Sorted tail boundary

After each pass, the largest remaining value is already at the end.

arr.length - i - 1

5. Neighbor comparison

Compare two neighbors and only swap if the left value is larger.

arr[j] > arr[j + 1]

6. Swap line

This destructuring assignment exchanges the two neighboring values.

[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]

7. Return sorted array

Return the same array after the in-place sorting passes finish.

return arr

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Bubble Sort: Visual Walkthrough & Practice · Typorithm | ApplyDjinn