What is the list 7, 3, 9, 2, 5 after the first pass of a bubble sort (ascending)?
2.3.1(f) · Data structures and algorithms
Bubble and insertion sort
What is the list 4, 1, 6, 3, 8, 2 after the first pass of a bubble sort (ascending)?
What is the list 9, 8, 7, 6 after the first pass of a bubble sort (ascending)?
Insertion sort runs on 5, 2, 8, 1, 9. What is the list once the items at indexes 1 and 2 have both been inserted?
Insertion sort runs on 3, 6, 1, 4. What is the list once the items at indexes 1 and 2 have both been inserted?
A bubble sort with no early stop runs n − 1 passes of n − 1 comparisons. How many comparisons for 6 items?
What is the worst-case time complexity of bubble sort?
Why is insertion sort fast on data that is already nearly sorted?
Bubble sort and insertion sort both sort in place. What does that mean?
Bubble sort
The procedure sorts an array into ascending order. It is called on 5, 3, 8, 4, 2.
procedure bubble(byRef a, n)swapped = Truewhile swappedswapped = Falsefor j = 0 to n - 2if a[j] > a[j + 1] thentemp = a[j]a[j] = a[j + 1]a[j + 1] = tempswapped = Trueendifnext jendwhileendprocedure
the table: the array and the number of swaps after each pass of the while loop.
| pass | a[0] | a[1] | a[2] | a[3] | a[4] | swaps |
|---|---|---|---|---|---|---|
| 1 | 3 | 5 | 4 | 2 | 8 | 3 |
the purpose of the variable swapped.
why temp is needed.
how many comparisons pass 1 makes.