Bubble and selection sort
Practice Flashcards
Flip to reveal answersHow does bubble sort work?
Track your progress — Sign up free to save your progress and get smart review reminders based on spaced repetition.
All 5 Flashcards — Bubble and selection sort
Sign up free to track progress and get spaced-repetition review schedules.
Question
How does bubble sort work?
Answer
It compares each neighbouring pair and swaps any that are out of order. Each pass sends the largest remaining value to the end, so the next pass can compare one fewer pair.
Question
How does selection sort work?
Answer
For each position it looks through the whole remaining list, remembering where the smallest value is, then swaps it into place. At most one swap per position.
Question
How do the two sorts compare on efficiency?
Answer
Both make O(n²) comparisons. Bubble sort makes up to O(n²) swaps; selection sort makes only O(n). Bubble sort has a best case of O(n) on sorted data thanks to its early exit, while selection sort has none.
Question
What is the space complexity of bubble and selection sort?
Answer
Both are O(1). Each sorts in place, needing only a couple of extra variables however large the list becomes.
Question
When is selection sort the better choice?
Answer
When writing data is far more expensive than reading it. The comparison counts are identical, so the algorithm doing O(n) swaps instead of O(n²) is clearly better.
Read the notes
Full study notes for Bubble and selection sort
Topic 6.4 hub
Programming algorithms
More from Topic 6.4
All flashcards in this topic
Computer Science exam skills
Paper structures & tips
Track your progress with spaced repetition
Sign up free — Aimnova tells you exactly which cards to review and when, so you remember everything before your IB exam.
Start Free