Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Bonus · not in the official syllabusChecked by code · teacher review pending

Bubble sort (enrichment)

Enrichment: repeatedly swap adjacent out-of-order pairs.

Syllabus says: “Not in the PCCST303 syllabus — supplementary”

On this page · 9 sections
ormakkurippu ✎
Never-forget card

Compare NEIGHBOURS, swap if out of order; the biggest bubbles to the end each pass. No swaps in a pass → already sorted, stop.

Picture it

Soda bubbles rising: the biggest bubble reaches the top first.

Write this in the exam

Bubble sort repeatedly compares adjacent elements and swaps them if they are in the wrong order; after pass i the i largest elements are in their final places. (Enrichment — not in the PCCST303 syllabus.)

Why this is here

Watch out

Bubble sort is NOT part of the PCCST303 syllabus. It is included as supplementary material because many students meet it first.

Definition

Each pass compares a[j] and a[j+1] for every j and swaps them if out of order, so the largest remaining value “bubbles” to the end. With an early-exit flag, a pass with no swaps ends the sort.

Worked example

Worked example: 5, 1, 4, 2, 8checked by code
Input5 1 4 2 8
After pass 11 4 2 5 8
After pass 21 2 4 5 8
After pass 31 2 4 5 8

Totals: comparisons = 9, swaps = 4

Now watch it run

Every frame comes from really running the algorithm on your input — change it and break it! Tap ▶ Play, or step one frame at a time with ◀ ▶.

Loading visualizer…

Algorithm

AlgorithmBubble Sort
  1. Step 1:
    Start.
  2. Step 2:
    Read the number of elements n.
  3. Step 3:
    Read the n elements into the array A.
  4. Step 4:
    Repeat for i = 0 to n-2:
    • Repeat for j = 0 to n-i-2: if A[j] > A[j+1], swap A[j] and A[j+1] (temp = A[j], A[j] = A[j+1], A[j+1] = temp).
  5. Step 5:
    Display the sorted array A.
  6. Step 6:
    Stop.

This is the plain lab-record version. Adding a flag that stops when a pass makes no swap gives the O(n) best case.

Pseudocode used by the animated lab · the lines highlighted in the visualizer
Bubble sort (enrichment)
  1. 1BubbleSort(a, n)
  2. 2for pass ← 0 to n − 2
  3. 3for j ← 0 to n − 2 − pass: if a[j] > a[j+1]
  4. 4swap a[j], a[j+1]; swapped ← true
  5. 5if not swapped: stop
  6. 6end

Complexity

Best O(n) with the early-exit flag (already sorted), average and worst O(n²). Stable, in-place.

Quick check — can you get them all?

Question 1

Q1.Bubble sort with the early-exit flag on sorted input costs…

Question 2

Q2.Is bubble sort stable?

Question 3

Q3.After pass 1 on 5, 1, 4, 2, 8:

Practice

Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.

References

  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3/e — Ch. 2, 6, 7, 8, 11 Problem 2-2

Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.

Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)