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 its correct position 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 during 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?
AMoves the largest unsorted element to its correct position
BFinds the smallest element and places it at the start
CDivides the array into two halves
DBuilds a sorted list by inserting elements
✗ 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 for n elements?
AO(n log n)
BO(n²)
CO(n)
DO(log n)
✗ Incorrect
Bubble Sort has a worst-case time complexity of O(n²) because it compares pairs repeatedly for each element.
Which of the following can optimize Bubble Sort to stop early?
ASkipping every other element
BSorting only half the array
CUsing a flag to detect no swaps in a pass
DUsing recursion
✗ Incorrect
If no swaps happen during a pass, the array is sorted and the algorithm can stop early.
In Bubble Sort, what happens if two adjacent elements are already in order?
AThey are left as is
BThey are swapped
CThe algorithm restarts
DThe elements are removed
✗ Incorrect
If adjacent elements are in correct order, no swap is done and the algorithm moves on.
How many comparisons does Bubble Sort make in the first pass for an array of size n?
An
Bn/2
C1
Dn-1
✗ Incorrect
In the first pass, Bubble Sort compares each adjacent pair, so it makes n-1 comparisons.
Explain how Bubble Sort works step-by-step on an unsorted array.
Think about how the largest number "bubbles" to the end.
You got /5 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. Insert each element into its correct position in a sorted sublist
B. Repeatedly swap adjacent elements if they are in the wrong order
C. Use a pivot to partition the list into smaller parts
D. Divide the list into halves and sort each half separately
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: insertion sort (A), divide and conquer (B), quicksort (C).
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 quicksort or merge sort
Thinking bubble sort divides the list
Assuming bubble sort inserts elements directly
2. Which of the following is the correct syntax to swap two elements arr[i] and arr[i+1] in Java during bubble sort?
easy
A. int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp;
B. arr[i] = arr[i+1]; arr[i+1] = arr[i];
C. swap(arr[i], arr[i+1]);
D. int temp = arr[i+1]; arr[i] = arr[i+1]; arr[i+1] = temp;
Solution
Step 1: Understand swapping in Java arrays
To swap, use a temporary variable to hold one value, then assign values accordingly.
Step 2: Check each option
int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; correctly uses a temp variable. arr[i] = arr[i+1]; arr[i+1] = arr[i]; overwrites arr[i] before saving it. swap(arr[i], arr[i+1]); is invalid syntax in Java. int temp = arr[i+1]; arr[i] = arr[i+1]; arr[i+1] = temp; overwrites arr[i] without saving original arr[i].
Final Answer:
int temp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = temp; -> Option A
Quick Check:
Use temp variable to swap [OK]
Hint: Always use a temp variable to swap two array elements [OK]
Common Mistakes:
Overwriting one element before saving it
Using a swap function that doesn't exist in Java
Swapping without a temporary variable
3. What will be the output array after one full pass of bubble sort on int[] arr = {5, 3, 8, 4, 2}?
medium
A. [3, 5, 8, 4, 2]
B. [2, 3, 4, 5, 8]
C. [5, 3, 4, 2, 8]
D. [3, 5, 4, 2, 8]
Solution
Step 1: Perform one pass of bubble sort
Compare and swap adjacent elements if needed: 5 and 3: swap -> [3,5,8,4,2] 5 and 8: no swap 8 and 4: swap -> [3,5,4,8,2] 8 and 2: swap -> [3,5,4,2,8]
Step 2: Result after first pass
The largest element 8 moves to the end. The array is [3,5,4,2,8].
Final Answer:
[3, 5, 4, 2, 8] -> Option D
Quick Check:
One pass moves largest to end [OK]
Hint: One pass pushes largest element to the end [OK]
Common Mistakes:
Not swapping all needed pairs in one pass
Assuming full sort after one pass
Confusing order of swaps
4. Identify the error in the following bubble sort code snippet:
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
medium
A. Inner loop should run till arr.length - i - 1 to avoid index out of bounds
B. Outer loop should start from 1 instead of 0
C. Swap logic is incorrect, should swap arr[j+1] with arr[j-1]
D. No error, code is correct
Solution
Step 1: Analyze inner loop range
Inner loop runs till arr.length, but accesses arr[j+1]. When j = arr.length - 1, arr[j+1] causes index out of bounds.
Step 2: Correct inner loop limit
Inner loop should run till arr.length - i - 1 to avoid out of bounds and optimize sorting.
Final Answer:
Inner loop should run till arr.length - i - 1 to avoid index out of bounds -> Option A
Quick Check:
Prevent arr[j+1] out of bounds by limiting inner loop [OK]
Hint: Inner loop limit must prevent arr[j+1] overflow [OK]
Common Mistakes:
Ignoring array index limits in inner loop
Incorrect swap indices
Assuming code runs without error
5. Given an array int[] arr = {4, 2, 7, 1, 3}, how many total swaps will bubble sort perform to fully sort the array?