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²) because it compares and swaps elements in nested loops.
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 stops 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?
ASorts the entire array in one pass
BMoves the smallest unsorted element to the beginning
CMoves the largest unsorted element to the end
DRemoves duplicates from 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?
AO(n²)
BO(n log n)
CO(log n)
DO(n)
✗ Incorrect
Bubble Sort uses nested loops, resulting in O(n²) time complexity in the worst case.
Which of these is a simple optimization for Bubble Sort?
AStop if no swaps happen in a pass
BUse binary search to find elements
CSort only half the array
DUse recursion instead of loops
✗ Incorrect
If no swaps occur in a pass, the array is already sorted, so Bubble Sort can stop early.
After the first pass of Bubble Sort on an unsorted array, what can we say about the last element?
AIt is the smallest element
BIt is in the middle
CIt is unchanged
DIt is the largest element
✗ Incorrect
The largest element "bubbles" to the end after the first pass.
Which data structure does Bubble Sort work on?
ALinked list only
BArray or list
CTree
DGraph
✗ Incorrect
Bubble Sort works on arrays or lists where elements can be accessed by index.
Explain how Bubble Sort works step-by-step on a small array.
Think about how the biggest number moves to the right in each pass.
You got /4 concepts.
Describe one way to improve Bubble Sort's efficiency.
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 array 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 move it to the beginning
D. Insert each element into its correct position in a sorted part
Solution
Step 1: Understand Bubble Sort process
Bubble Sort compares adjacent elements and swaps them if they are out of order.
Step 2: Identify the correct description
This swapping of neighbors repeats until the whole list is sorted.
Final Answer:
Repeatedly swap adjacent elements if they are in the wrong order -> Option B
Quick Check:
Bubble Sort = Swap neighbors repeatedly [OK]
Hint: Bubble Sort swaps neighbors repeatedly until sorted [OK]
Common Mistakes:
Confusing Bubble Sort with Selection Sort
Thinking it divides the array like Merge Sort
Assuming it inserts elements like Insertion Sort
2. Which of the following is the correct syntax for the inner loop of Bubble Sort in C++ to compare adjacent elements in an array arr of size n?
easy
A. for (int j = 0; j < n - i - 1; j++)
B. for (int j = 0; j < n; j++)
C. for (int j = 1; j < n - i; j++)
D. for (int j = 0; j <= n - i; j++)
Solution
Step 1: Understand inner loop range
The inner loop runs from 0 to n - i - 1 because after each pass, the last i elements are sorted.
Step 2: Check each option
for (int j = 0; j < n - i - 1; j++) correctly uses j < n - i - 1 to avoid out-of-bound and unnecessary comparisons.
Final Answer:
for (int j = 0; j < n - i - 1; j++) -> Option A
Quick Check:
Inner loop limit = n - i - 1 [OK]
Hint: Inner loop stops before sorted elements at end [OK]
Common Mistakes:
Using j < n instead of j < n - i - 1
Starting j from 1 instead of 0
Using <= instead of < causing out-of-bound
3. What will be the output of the following C++ code snippet?
int arr[] = {5, 3, 8, 4};
int n = 4;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}