Photo AI
Last Updated Sep 27, 2025
Revision notes with simplified explanations to understand Bubble Sort quickly and effectively.
241+ students studying
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.
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.
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
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]
Example: Given 6, 2, 4, 8
swapped
flag.swapped
flag to optimise performance and avoid unnecessary passes.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 Flashcards12 quizzes
Quizzes on Bubble Sort
Test your knowledge with fun and engaging quizzes.
Try Computer Science Quizzes29 questions
Exam questions on Bubble Sort
Boost your confidence with real exam questions.
Try Computer Science Questions27 exams created
Exam Builder on Bubble Sort
Create custom exams across topics for better practice!
Try Computer Science exam builder12 papers
Past Papers on Bubble Sort
Practice past papers to reinforce exam experience.
Try Computer Science Past PapersDiscover More Revision Notes Related to Bubble Sort to Deepen Your Understanding and Improve Your Mastery
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!
Report Improved Results
Recommend to friends
Students Supported
Questions answered