Recall & Review
beginner
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 is O(n²) in both average and worst cases because it compares pairs repeatedly for each element.
Click to reveal answer
intermediate
How can Bubble Sort be optimized to stop early if the array is already sorted?
By adding 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
Write the basic step of Bubble Sort comparing two adjacent elements a[i] and a[i+1].
If a[i] > a[i+1], swap a[i] and a[i+1] to move the larger element towards the end.
Click to reveal answer
What does Bubble Sort do in each pass through the array?
✗ Incorrect
Bubble Sort compares adjacent elements and swaps them to move the largest unsorted element to the end in each pass.
What is the worst-case time complexity of Bubble Sort?
✗ Incorrect
Bubble Sort has a worst-case time complexity of O(n²) because it compares pairs repeatedly for each element.
Which of these is a way to optimize Bubble Sort?
✗ Incorrect
If no swaps happen during a pass, the array is already sorted and Bubble Sort can stop early.
In Bubble Sort, what happens when two adjacent elements are in the wrong order?
✗ Incorrect
Bubble Sort swaps adjacent elements if they are in the wrong order to sort the array.
How many passes does Bubble Sort need to sort an array of 5 elements in the worst case?
✗ Incorrect
Bubble Sort needs n-1 passes, so for 5 elements, it needs 4 passes in the worst case.
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 make Bubble Sort faster when the array is already mostly sorted.
How do you know if the array is sorted before finishing all passes?
You got /3 concepts.