What is the main idea behind the Bubble Sort algorithm?
Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order, "bubbling" the largest unsorted element to the end in each pass.
Click to reveal answer
beginner
How many passes does Bubble Sort make in the worst case for an array of size n?
In the worst case, Bubble Sort makes n-1 passes through the array to fully sort it.
Click to reveal answer
intermediate
What is the time complexity of Bubble Sort in the average and worst cases?
The time complexity of Bubble Sort is O(n²) in both average and worst cases because it compares pairs repeatedly in nested loops.
Click to reveal answer
intermediate
What optimization can be added to Bubble Sort to stop early if the array is already sorted?
Add a flag to check if any swaps happened in a pass. If no swaps occur, the array is sorted and the algorithm can stop early.
Click to reveal answer
beginner
Show the state of the array [5, 3, 8, 4, 2] after the first pass of Bubble Sort.
After first pass: [3, 5, 4, 2, 8]. The largest element 8 has bubbled to the end.
Click to reveal answer
What does Bubble Sort compare during its sorting process?
ARandom elements
BFirst and last elements
CAdjacent elements
DOnly the smallest elements
✗ Incorrect
Bubble Sort compares adjacent elements to decide if they need to be swapped.
What is the best case time complexity of Bubble Sort when optimized with a swap flag?
AO(log n)
BO(n log n)
CO(n²)
DO(n)
✗ Incorrect
With the swap flag optimization, if the array is already sorted, Bubble Sort completes in O(n) time.
Which of these is NOT true about Bubble Sort?
AIt is a stable sorting algorithm
BIt has a time complexity of O(n log n) in the worst case
CIt sorts by repeatedly swapping adjacent elements
DIt can be optimized to stop early if no swaps occur
✗ Incorrect
Bubble Sort has O(n²) time complexity in the worst case, not O(n log n).
After how many passes is the largest element guaranteed to be at the end of the array in Bubble Sort?
AAfter the first pass
BAfter the last pass
CAfter half the passes
DIt never moves
✗ Incorrect
Each pass moves the largest unsorted element to its correct position at the end.
Which data structure is Bubble Sort best suited for?
ASmall or nearly sorted arrays
BLarge unsorted arrays
CLinked lists only
DTrees
✗ Incorrect
Bubble Sort is simple and works best on small or nearly sorted arrays due to its simplicity and early stopping optimization.
Explain how Bubble Sort works step-by-step on a small array.
Think about how bubbles rise to the surface in water.
You got /4 concepts.
Describe one way to optimize Bubble Sort and why it helps.
Consider what happens if the array is already sorted.
You got /4 concepts.
Practice
(1/5)
1. What is the main idea behind the Bubble Sort algorithm?
easy
A. Divide the list into halves and sort each half separately
B. Repeatedly swap adjacent elements if they are in the wrong order
C. Select the smallest element and place it at the beginning
D. Use a pivot to partition the list into smaller parts
Solution
Step 1: Understand Bubble Sort process
Bubble Sort compares adjacent elements and swaps them if they are out of order.
Step 2: Compare with other sorting methods
Other options describe different algorithms: divide and conquer, selection, and quicksort.
Final Answer:
Repeatedly swap adjacent elements if they are in the wrong order -> Option B
Quick Check:
Bubble Sort = Swap adjacent pairs [OK]
Hint: Bubble Sort swaps neighbors repeatedly until sorted [OK]
Common Mistakes:
Confusing Bubble Sort with Selection Sort
Thinking it divides the list like Merge Sort
Mixing it up with Quick Sort pivot method
2. Which of the following is the correct syntax to start a bubble sort outer loop in Python for a list arr of length n?
easy
A. for i in range(n):
B. for i in range(1, n):
C. for i in range(n-1):
D. for i in range(n+1):
Solution
Step 1: Understand loop range for bubble sort
The outer loop runs from 0 to n-2 because we compare pairs up to the second last element.
Step 2: Check each option's range
for i in range(n-1): uses range(n-1), which means 0 to n-2, correct for bubble sort outer loop.
Final Answer:
for i in range(n-1): -> Option C
Quick Check:
Outer loop runs n-1 times [OK]
Hint: Outer loop runs n-1 times for bubble sort [OK]
Common Mistakes:
Using range(n) causes extra unnecessary iteration
Starting loop at 1 misses first element
Using range(n+1) causes index errors
3. What will be the output of the following code?
arr = [4, 2, 3]
for i in range(len(arr)-1):
for j in range(len(arr)-1-i):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
print(arr)
medium
A. [2, 3, 4]
B. [4, 3, 2]
C. [3, 2, 4]
D. [2, 4, 3]
Solution
Step 1: Trace first outer loop iteration (i=0)
Inner loop runs j=0 to 1. Swaps 4 and 2 -> [2,4,3], then swaps 4 and 3 -> [2,3,4].
Step 2: Trace second outer loop iteration (i=1)
Inner loop runs j=0 only. Compares 2 and 3, no swap needed. Array stays [2,3,4].
Final Answer:
[2, 3, 4] -> Option A
Quick Check:
Bubble Sort sorts ascending -> [2,3,4] [OK]
Hint: Bubble Sort outputs sorted list ascending [OK]
Common Mistakes:
Not swapping correctly in inner loop
Confusing loop ranges causing partial sort
Printing before sorting completes
4. Identify the error in this bubble sort code snippet:
def bubble_sort(arr):
n = len(arr)
for i in range(n-1):
for j in range(n-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
medium
A. Inner loop should run till n-1-i, not n-1
B. Outer loop should run till n-1, not n
C. Swap syntax is incorrect
D. Function does not return anything
Solution
Step 1: Analyze inner loop range
Inner loop runs from 0 to n-2 always, not reducing with i, causing unnecessary comparisons.
Step 2: Correct inner loop range
It should be range(n-1-i) to avoid checking already sorted elements at the end.
Final Answer:
Inner loop should run till n-1-i, not n-1 -> Option A
Quick Check:
Inner loop shrinks each pass [OK]
Hint: Inner loop shrinks by i each outer iteration [OK]
Common Mistakes:
Not reducing inner loop range wastes time
Incorrect swap syntax causes errors
Forgetting to return sorted array
5. Given the list arr = [5, 1, 4, 2, 8], how many total swaps will bubble sort perform to sort it in ascending order?