0
0
DSA Javascriptprogramming~5 mins

Bubble Sort Algorithm in DSA Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AMoves the smallest element to the start
BRemoves duplicates from the array
CSorts the entire array at once
DMoves the largest unsorted element to the end
What is the worst-case time complexity of Bubble Sort?
AO(n)
BO(n log n)
CO(n²)
DO(log n)
Which of these is a way to optimize Bubble Sort?
AUse recursion instead of loops
BStop if no swaps happen in a pass
CSort only half the array
DSkip comparing adjacent elements
In Bubble Sort, what happens when two adjacent elements are in the wrong order?
AThey are swapped
BThey are removed
CThey are ignored
DThey are duplicated
How many passes does Bubble Sort need to sort an array of 5 elements in the worst case?
A4
B6
C5
D3
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.