This preview shows pages 1–3. Sign up to view the full content.
1.
a.
What is the total number of comparisons and the total number of swaps
(comparing outoforder pairs) of adjacent elements in the following list for both
bubble and insertion sort.
Bubble Sort
Compares = 0 Swaps = 0
L: 8, 5, 7, 2, 3
Compares = 1 Swaps = 1
L: 5, 8, 7, 2, 3
Compares = 2 Swaps = 2
L: 5, 7, 8, 2, 3
Compares = 3 Swaps = 3
L: 5, 7, 2, 8, 3
Compares = 4 Swaps = 4
L: 5, 7, 2, 3, 8
Compares = 5 Swaps = 4
L: 5, 7, 2, 3, 8
Compares = 6 Swaps = 5
L: 5, 2, 7, 3, 8
Compares = 7 Swaps = 6
L: 5, 2, 3, 7, 8
Compares = 8 Swaps = 7
L: 2, 5, 3, 7, 8
Compares = 9 Swaps = 8
L: 2, 3, 5, 7, 8
Compares = 10 Swaps = 8
L: 2, 3, 5, 7, 8
Final Answer: Compares = 10 Swaps = 8
Insertion Sort
Compares = 0 Swaps = 0
L: 8, 5, 7, 2, 3
Compares = 1 Swaps = 1
L: 5, 8, 7, 2, 3
Compares = 2 Swaps = 2
L: 5, 7, 8, 2, 3
Compares = 3 Swaps = 2
L: 5, 7, 8, 2, 3
Compares = 4 Swaps = 3
L: 5, 7, 2, 8, 3
Compares = 5 Swaps = 4
L: 5, 2, 7, 8, 3
Compares = 6 Swaps = 5
L: 2, 5, 7, 8, 3
Compares = 7 Swaps = 6
L: 2, 5, 7, 3, 8
Compares = 8 Swaps = 7
L: 2, 5, 3, 7, 8
Compares = 9 Swaps = 8
L: 2, 3, 5, 7, 8
Compares = 10 Swaps = 8
L: 2, 3, 5, 7, 8
Final Answer: Compares = 10 Swaps = 8
b.
Why does the number of swaps give a lower bound on computation time for these
two sorts?
Bubble Sort give a lower bound on computation time compared to the number of swaps
because the bigger numbers are at the front and it takes a lot more swaps to move them to
the end because the way Bubble Sort swaps elements. Since every element is at most N
1 away from its final position, the biggest number of operations to move the element to
its sorted spot is n  1 = O(n) operations. For Insertion Sort a lower bound computation is
given based on the number of swaps because on average and in worse case it takes
O(n^2). So more times than not the number of swaps will be a lower bound computation
because contrary to bubble sort which takes less time than the previous pass, insertion
may take more time on each pass than the previous.
c.
What is the number of inversions in list L? Why is the number of swaps of adjacent
elements equal to the number of inversions for these sorts?
There are 8 inversions in list L. The number of inversions is always the number of swaps
because swapping two adjacent elements that are out of place removes one inversion.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document d.
Why is the total number of inversions in the first two lists below equal to the
number in the third list below?
L: 8, 5, 7, 2, 3 Lreverse: 3, 2, 7, 5, 8 L3descending: 8, 7, 5, 3, 2
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 03/31/2008 for the course CSE 331 taught by Professor M.mccullen during the Spring '08 term at Michigan State University.
 Spring '08
 M.McCullen
 Bubble Sort, Insertion Sort

Click to edit the document details