Bubble sort repeatedly steps through a list, and on each pass compares each pair of neighbouring elements. What does it do when it finds a pair that is in the wrong order?
- It removes one of the two elements from the list entirely
- It moves the smaller element all the way to the very front of the whole list immediately
- It swaps the two neighbouring elements, then continues comparing the next pair further along the list
- It stops the entire sorting process and restarts from the very beginning of the list
Why C? And why not the others?
Correct answer: C. It swaps the two neighbouring elements, then continues comparing the next pair further along the list
Bubble sort moves left to right through a list comparing each pair of neighbouring elements one at a time; whenever the left element is greater than the right one (for ascending order), it swaps just those two neighbours and then carries straight on to compare the next adjacent pair further along, repeating full passes over the list until an entire pass completes with no swaps, at which point the list is sorted, with larger values gradually 'bubbling' toward the end across successive passes. The option describing removing an element is wrong because sorting never deletes anything; every element that starts in the list is still present at the end, only rearranged. The option describing moving the smaller element all the way to the front in one step is wrong because bubble sort only ever swaps two adjacent elements per comparison, it never relocates an element across the whole list in a single move. The option describing stopping and restarting from the beginning is wrong because bubble sort does not abandon a pass after a swap; it keeps comparing forward along the same pass, only beginning a fresh pass once it reaches the end of the list.
Source: BBC Bitesize GCSE Computer Science: Sorting algorithms - bubble sort