Python Tutorial

Bubble Sort

Repeatedly swap adjacent out-of-order pairs so the largest values 'bubble' to the end.

How Bubble Sort Works

Bubble sort walks the list comparing each pair of neighbors and swapping them if they are in the wrong order. After the first pass the largest element sits at the end. Repeat on the shrinking unsorted portion until no swaps are needed.

It is the simplest sort to understand but among the slowest at O(n²) — used mainly for teaching.

Basic Implementation

def bubble_sort(arr):
    n = len(arr)
    for i in range(n - 1):
        for j in range(n - 1 - i):        # last i items already sorted
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
    return arr

print(bubble_sort([64, 34, 25, 12, 22, 11, 90]))
# [11, 12, 22, 25, 34, 64, 90]

Optimized: Stop Early

If a full pass makes no swaps, the list is already sorted. Tracking that flag makes the best case (already-sorted input) O(n).

def bubble_sort_fast(arr):
    n = len(arr)
    for i in range(n - 1):
        swapped = False
        for j in range(n - 1 - i):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
                swapped = True
        if not swapped:        # nothing moved -> done
            break
    return arr

Complexity

CaseTimeNotes
Best (sorted, optimized)O(n)One clean pass, no swaps
AverageO(n²)
Worst (reverse sorted)O(n²)Every pair swaps
SpaceO(1)Sorts in place

Bubble sort is stable (equal elements keep their relative order) and in-place.

Why It Is Rarely Used

Even the optimized version makes O(n²) comparisons on average — impractical beyond small inputs. In real Python, always use the built-in Timsort:

data = [64, 34, 25, 12]
data.sort()                 # in place, O(n log n)
new = sorted(data, reverse=True)   # returns a new sorted list

Best Practices

  • Learn bubble sort for intuition, but use sorted()/list.sort() in real code.
  • Add the early-exit flag whenever you must implement it.
  • For nearly-sorted data, insertion sort is a better simple choice.

Try It Yourself

Exercise 1: After one full pass of bubble sort on [5, 1, 4, 2], what is the array?

Show solution

[1, 4, 2, 5] — the largest value (5) has bubbled to the end.

Exercise 2: What does the "swapped" flag optimization achieve?

Show solution

It stops early when a pass makes no swaps, giving O(n) best-case time on already-sorted data.

📘 Real-World Deep Dive

Knowing <strong>DSA Bubble Sort (algorithms & data structures)</strong> well is what turns algorithms & data structures from a curiosity into a daily tool — you'll reach for it in nearly every real project.

Real-Life Scenario

An end-to-end usage of DSA Bubble Sort that you'd actually see in a data pipeline or analytics notebook.

Real-Life Example

def bubble(xs):
    n = len(xs)
    for i in range(n):
        for j in range(0, n - i - 1):
            if xs[j] > xs[j+1]:
                xs[j], xs[j+1] = xs[j+1], xs[j]
    return xs

print(bubble([5, 2, 9, 1, 5, 6]))

Expected Output

(see source)

Common mistakes

  • Off-by-one errors in binary-search: the standard idiom is while lo <= hi with mid = (lo + hi) // 2.
  • Recursive algorithms blow the stack for n > ~10⁴; convert to iterative with an explicit stack.
  • Comparison algorithms (sorted(iterable)) are stable by default in Python — surprising for Java/C++ users.
  • Treating DSA Bubble Sort as a black box without reading the docs — the API has subtle defaults that bite when you scale.

🚀 Performance & Best Practices

  • Use bisect.bisect_left / bisect_right instead of writing your own binary search.
  • Convert a sorted search into a tuple-access pattern with numpy.searchsorted for huge arrays.
  • Use heapq for priority queues instead of maintaining a sorted list manually.
  • When working with algorithms & data structures, prefer vectorised / batched operations over Python loops.

🧪 Try It Yourself

  1. Reproduce the snippet on a representative slice of your own data.
  2. Profile the snippet with cProfile or timeit and find the single biggest improvement.
  3. Generalise the snippet into a small, reusable function you can drop into future projects.

FAQ: Bubble Sort

Common questions about this page.

What is Bubble Sort?

Bubble Sort is a DSA lesson that explains bubble sort in Python. Repeatedly swap adjacent out-of-order pairs so the largest values 'bubble' to the end. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run bubble sort examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn bubble sort in this DSA Python lesson (Bubble Sort).

How do I use bubble sort in Python?

To use bubble sort in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of bubble sort?

This Bubble Sort tutorial shows bubble sort syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Bubble Sort example for beginners

Yes. This page includes a beginner bubble sort example you can copy and run. It is designed for searches such as "bubble sort for beginners", "bubble sort example", and "how to use bubble sort".

What are common mistakes with bubble sort?

Common bubble sort mistakes include wrong syntax, mixing types, and skipping practice. Work through this DSA chapter in order, run every example, and check the output before moving on.

Why should I learn bubble sort?

Bubble Sort is used in real Python work. Learning bubble sort helps you write clearer programs and continue the DSA tutorial on StudyGrid.

Is Bubble Sort free to learn online?

Yes. You can learn bubble sort free on StudyGrid (studygrid.in). This chapter is part of the DSA path and includes examples, syntax, and next-step links.