Photo AI

Last Updated Sep 27, 2025

Bubble Sort Simplified Revision Notes

Revision notes with simplified explanations to understand Bubble Sort quickly and effectively.

user avatar
user avatar
user avatar
user avatar
user avatar

241+ students studying

Bubble Sort

Overview

Bubble Sort is a simple sorting algorithm that repeatedly compares adjacent elements in a list and swaps them if they are in the wrong order. This process continues until the list is sorted. Although inefficient for large datasets, Bubble Sort is useful for understanding basic sorting concepts and small data sets.

How Bubble Sort Works

  • Step 1: Compare the first two adjacent elements.
  • Step 2: If the first element is greater than the second, swap them.
  • Step 3: Move to the next pair of adjacent elements and repeat.
  • Step 4: Continue until the end of the list. This completes one pass.
  • Step 5: Repeat the process for multiple passes until no swaps are needed, meaning the list is sorted.

Properties of Bubble Sort

  • Stable: Maintains the relative order of equal elements.
  • In-Place: Requires only a small, constant amount of extra memory.
  • Time Complexity:
    • Best Case: O(n) (when the list is already sorted).
    • Worst Case: O(n²) (when the list is sorted in reverse order).
    • Average Case: O(n²).
infoNote

Example of Bubble Sort

Unsorted List:

5, 3, 8, 4, 2

Pass 1:

  • Compare 5 and 3 → Swap → 3, 5, 8, 4, 2

  • Compare 5 and 8 → No Swap → 3, 5, 8, 4, 2

  • Compare 8 and 4 → Swap → 3, 5, 4, 8, 2

  • Compare 8 and 2 → Swap → 3, 5, 4, 2, 8 Pass 2:

  • Compare 3 and 5 → No Swap → 3, 5, 4, 2, 8

  • Compare 5 and 4 → Swap → 3, 4, 5, 2, 8

  • Compare 5 and 2 → Swap → 3, 4, 2, 5, 8

  • Compare 5 and 8 → No Swap → 3, 4, 2, 5, 8 Pass 3:

  • Compare 3 and 4 → No Swap → 3, 4, 2, 5, 8

  • Compare 4 and 2 → Swap → 3, 2, 4, 5, 8

  • Compare 4 and 5 → No Swap → 3, 2, 4, 5, 8

  • Compare 5 and 8 → No Swap → 3, 2, 4, 5, 8 Pass 4:

  • Compare 3 and 2 → Swap → 2, 3, 4, 5, 8

  • Compare 3 and 4 → No Swap → 2, 3, 4, 5, 8

  • Compare 4 and 5 → No Swap → 2, 3, 4, 5, 8

  • Compare 5 and 8 → No Swap → 2, 3, 4, 5, 8 At this point, no swaps are needed, and the list is sorted.

Pseudocode for Bubble Sort

METHOD BubbleSort(array)
    n ← array.length
    FOR i FROM 0 TO n - 2
        swapped ← FALSE
        FOR j FROM 0 TO n - i - 2
            IF array[j] > array[j + 1]
                SWAP array[j] AND array[j + 1]
                swapped ← TRUE
        ENDFOR
        IF NOT swapped
            BREAK
    ENDFOR

Python Example

def bubble_sort(array):
    n = len(array)
    for i in range(n - 1):
        swapped = False
        for j in range(n - i - 1):
            if array[j] > array[j + 1]:
                array[j], array[j + 1] = array[j + 1], array[j]  # Swap
                swapped = True
        if not swapped:
            break  # If no swaps occurred, the list is sorted
    return array

# Example usage:
data = [5, 3, 8, 4, 2]
sorted_data = bubble_sort(data)
print(sorted_data)  # Output: [2, 3, 4, 5, 8]

Tracing Bubble Sort

lightbulbExample

Example: Given 6, 2, 4, 8

  1. Initial List: 6, 2, 4, 8
  2. Pass 1:
  • Compare 6 and 2 → Swap → 2, 6, 4, 8
  • Compare 6 and 4 → Swap → 2, 4, 6, 8
  • Compare 6 and 8 → No Swap → 2, 4, 6, 8
  1. Pass 2:
  • Compare 2 and 4 → No Swap → 2, 4, 6, 8
  • Compare 4 and 6 → No Swap → 2, 4, 6, 8
  • Compare 6 and 8 → No Swap → 2, 4, 6, 8 Since no swaps occurred in Pass 2, the list is sorted.

Note Summary

infoNote

Common Mistakes

  1. Not Accounting for Sorted Data: Continuing passes even when the list is already sorted increases unnecessary comparisons. This is resolved by using the swapped flag.
  2. Incorrect Loop Bounds: Forgetting to decrease the range of inner loops as the largest elements are already sorted in the last positions.
  3. Not Implementing Early Exit: Without checking if swaps occurred in a pass, the algorithm may continue even when the list is sorted.
infoNote

Key Takeaways

  • Bubble Sort is a simple, yet inefficient, sorting algorithm with a worst-case complexity of O(n²).
  • It works by repeatedly swapping adjacent elements until the list is sorted.
  • It is easy to implement and understand but not practical for large datasets.
  • Use the swapped flag to optimise performance and avoid unnecessary passes.
Books

Only available for registered users.

Sign up now to view the full note, or log in if you already have an account!

500K+ Students Use These Powerful Tools to Master Bubble Sort

Enhance your understanding with flashcards, quizzes, and exams—designed to help you grasp key concepts, reinforce learning, and master any topic with confidence!

120 flashcards

Flashcards on Bubble Sort

Revise key concepts with interactive flashcards.

Try Computer Science Flashcards

12 quizzes

Quizzes on Bubble Sort

Test your knowledge with fun and engaging quizzes.

Try Computer Science Quizzes

29 questions

Exam questions on Bubble Sort

Boost your confidence with real exam questions.

Try Computer Science Questions

27 exams created

Exam Builder on Bubble Sort

Create custom exams across topics for better practice!

Try Computer Science exam builder

12 papers

Past Papers on Bubble Sort

Practice past papers to reinforce exam experience.

Try Computer Science Past Papers

Other Revision Notes related to Bubble Sort you should explore

Discover More Revision Notes Related to Bubble Sort to Deepen Your Understanding and Improve Your Mastery

96%

114 rated

Algorithms for the Main Data Structures

Stacks

user avatar
user avatar
user avatar
user avatar
user avatar

333+ studying

192KViews

96%

114 rated

Algorithms for the Main Data Structures

Queues

user avatar
user avatar
user avatar
user avatar
user avatar

302+ studying

197KViews

96%

114 rated

Algorithms for the Main Data Structures

Trees

user avatar
user avatar
user avatar
user avatar
user avatar

370+ studying

181KViews

96%

114 rated

Algorithms for the Main Data Structures

Linked List

user avatar
user avatar
user avatar
user avatar
user avatar

497+ studying

184KViews
Load more notes

Join 500,000+ A-Level students using SimpleStudy...

Join Thousands of A-Level Students Using SimpleStudy to Learn Smarter, Stay Organized, and Boost Their Grades with Confidence!

97% of Students

Report Improved Results

98% of Students

Recommend to friends

500,000+

Students Supported

50 Million+

Questions answered