Bubble Sort Flashcards
All 5 cards in this deck
What are the main steps of a bubble sort?
e.g. sort 3, 1, 2- Compare the first pair and swap them if they are in the wrong order: 1, 3, 2.
- Move along one place and repeat to the end of the list (one pass): 1, 2, 3.
- Repeat passes until a pass makes no swaps: list is sorted as 1, 2, 3.
True or false? A bubble sort always swaps the pair of values it compares.
False. It swaps them only if they are in the wrong order.
In a bubble sort into ascending order, where does the largest value end up after the first pass?
At the end of the list, in its final position, so each later pass can stop one item sooner.
In a bubble sort, why is a
tempvariable used when two values are swapped?It holds a copy of the first value; otherwise that value would be overwritten and lost when the second value is copied into its place.
An algorithm has nested FOR loops that compare
arr[j]witharr[j + 1]and swap them if they are in the wrong order. Which algorithm is it?A bubble sort. Its purpose is to sort the array into order.