Bubble Sort

DSA · Sorting Algorithms

Theory

Bubble sort repeatedly walks through the list, compares neighbouring pairs and swaps them if they are in the wrong order. After each full pass the largest remaining element "bubbles" to its final position at the end. An early-exit check stops the algorithm when a pass makes no swaps, which makes it O(n) on already-sorted data. Its average and worst case are O(n²).

Why we use it

It is the easiest sorting algorithm to understand and is often used to teach how sorting and swapping work.

When to use it

Use it only for teaching or tiny lists. In real code use the built-in sort (Timsort) or merge/quick sort.

How it works

  1. Start: pass = 0
  2. For j = 0..n-pass-2: compare arr[j] and arr[j+1]
  3. arr[j] > arr[j+1]? Yes: Swap them. No: Leave them.
  4. Largest unsorted value has bubbled to the end
  5. Any swap in this pass? Yes: pass = pass + 1, next pass. No: Array is sorted: stop.
  6. Done

Syntax

repeat n-1 times:
    for j in 0..n-2-pass:
        if arr[j] > arr[j+1]: swap
    stop early if no swaps

Time: O(n²) avg/worst, O(n) best   Space: O(1)   Stable

Example

def bubble_sort(arr):
    n = len(arr)
    for p in range(n - 1):
        swapped = False
        for j in range(n - 1 - p):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
                swapped = True
        print("After pass", p + 1, ":", arr)
        if not swapped:
            break
    return arr

bubble_sort([64, 34, 25, 12, 22])

Key points

DSA topics

Introduction to DSA

Arrays and Searching

Sorting Algorithms

Linked Lists

Stacks and Queues

Hashing

Trees

Heaps

Graphs

Algorithm Techniques

Classic Algorithms

Dynamic Programming

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Data Structures & Algorithms

The complete DSA roadmap: theory, why and when to use every structure and algorithm, Big O complexity, and Python code you can run right here in the browser.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice DSA Online 251 problems across 13 patterns, with hidden tests and step-by-step optimal solutions. Start practising →
Section {{ sec.num }}

{{ sec.label }}

DSA  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ current.why }}

When to use it

{{ current.when }}

Flowchart — how {{ current.name }} works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Step-by-step visualisation
{{ cell.v }}
{{ cell.i }} {{ cell.tag }}
Step {{ vizStepLabel }}: {{ vizNote }}
■ comparing ■ swapping / moving ■ done / found ■ out of range
Complexity & Pseudocode
{{ current.syntax }}
Example Code
{{ current.example }}
▶ Try It Live — runs real Python 3 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic