Homework 20


Due : 9.30am, 9 August

Trace through the bubble-sort and selection-sort algorithms on the following data. Indicate also the number of array element comparisons and swaps for each case.

2 7 1 4 3 6

Solution

Bubble-Sort
ij startj end2 7 1 4 3 6 (initial)compsswaps
0511 2 7 3 4 653
1521 2 3 7 4 641
2531 2 3 4 7 631
3541 2 3 4 6 721
4551 2 3 4 6 710
Total : 156

Selection-Sort
BeginCheck startCheck end2 7 1 4 3 6 (initial)compsswaps
0151 7 2 4 3 651
1251 2 7 4 3 641
2351 2 3 4 7 631
3451 2 3 4 7 621
4551 2 3 4 6 711
Total : 155


Last updated : 9 August 2000 12.07pm