correct position to insert this new element. You should notice that for unique keys the standard binary search would always return a failure. So for this problem, assume a revised binary search that returns the location at which this key belongs.
a. What is the worst-case analysis of this new insertion sort?
b. What is the average-case analysis of this new insertion sort?
while swappedElements do
numberOfPairs = numberOfPairs - 1 swappedElements = false
for i = 1 to numberOfPairs do
if list[ i ] > list[ i + 1 ] then Swap( list[i], list[i + 1] ) swappedElements = true end if
end for end while
■ 3.2.1 Best-Case Analysis
We look quickly at the best case because the swappedElements flag might give the wrong impression of how this algorithm functions. Consider what will cause this algorithm to do the least amount of work. On the first pass, the for loop must fully execute, and so this algorithm does at least N 1 com- parisons. There are two possibilities that should be considered: There is at least one swap or there are no swaps. In the first case, the swap will cause swappedElements to be set to true, which will cause the while loop to execute a second time, which will do another N 2 comparisons. In the sec- ond case, because there are no swaps, swappedElements will still be false and the algorithm will end.
So the best case is N 1 comparisons, and this occurs when there are no swaps on the first pass. This means that the input data associated with the best case would be when the data values are already in order.
■ 3.2.2 Worst-Case Analysis
If the best case is when the input data starts in order, we might want to see if having the input data in reverse order will lead us to the worst case. If the larg- est value is first, it will be swapped with every other element down the list until it is in the last position. At the start of the first pass, the second largest ele- ment was in the second position, but you should see that the first comparison on the first pass swapped it into the first position of the list. At the start of the second pass, the second largest element is now in the first position, and it will be swapped with every other element in the list until it is in the second to last position. This gets repeated for every other element, and so the algorithm has
to execute the for loop N 1 times. This indicates that an input data set that is in the reverse order does lead to the worst case.1
How many comparisons are done in this worst case? We have said that the first pass will do N 1 comparisons of adjacent values, and the second pass will do N 2 comparisons. Further examination will show that each succes- sive pass reduces the number of comparisons by 1. This means that the worst case is given by the formula
■ 3.2.3 Average-Case Analysis
We have already said that in the worst case, there would be N 1 repetitions of the inner for loop. For the average case, we will assume that it is equally likely that on any of these passes there will be no swaps done. We need to know how many comparisons are done in each of these possibilities. If we stop after one pass, we have done N 1 comparisons. If we stop after two passes, we have done N 1 + N 2 comparisons. For now, let’s say that C(i) will calculate how many comparisons are done on the first i passes. Because the algorithm stops when there are no swaps done, the average case is found by looking at all of the places bubble sort can stop. This gives the equation
1 This case does the largest number of comparisons and swaps, but because we are only interested in counting the number of comparisons, there are other data sets that will lead us to this worst case. The reader should be able to show that any list of elements with the smallest element in the last position, for example, will also produce this worst- case result. Can you find any others?
W N( ) i
i=N–1
∑
1 i i=1N–1
∑
(---N–21)N N2–N ---2= = = =
W N( ) 1 2--N2
≈ = O N( 2)
A N( ) 1 N–1
--- C i( )
i=1 N–1
∑
=
where, again, C(i) is the number of comparisons done in the first i passes of the for loop before we stop. So, how many comparisons is this? C(i) is given by the equation
Substituting this back into the first equation gives the following
Because N is a constant relative to i, by using Equations 1.11 and 1.14 we can get
Again using Equation 1.11 and some basic algebra, we get
Now, we apply Equations 1.15 and 1.16 to get C i( ) j
j=N–1
∑
i j j=iN–1
∑
jj=1
N–1
∑
jj=1
∑
i–1– the last step by Equation 1.10
= = =
C i( ) (N–1)N
---2 (i–1)i ---2
– N2–N–i2+i
---2
= =
A N( ) 1 N–1
--- N2–N–i2+i ---2
i=1 N–1
∑
=
A N( ) 1 N–1
--- (N–1) N2–N
---2 –i2+i ---2
i=1 N–1
∑
* +
=
A N( ) N2–N
---2 1 2(N–1) --- –i2
i=1
N–1
∑
ii=1 N–1
∑
+ +
=
A N( ) N2–N
---2 1 2(N–1)
--- (N–1)N(2N–1)
---6 (N–1)N ---2 +
– +
=
A N( ) N2–N
---2 N(2N–1) ---12
– N
----4 +
=
A N( ) 6N2–6N–2N2+N+3N ---12
=
A N( ) 4N2–2N ---12
= A N( ) 1
3--N2
≈ = O N( 2)
3.2.4
1. Show the results of each pass of BubbleSort applied to the list [7, 3, 9, 4, 2, 5, 6, 1, 8].
2. Show the results of each pass of BubbleSort applied to the list [3, 5, 2, 9, 8, 1, 6, 4, 7].
3. A different version of bubble sort keeps track of where the last exchange occurred, and on the next pass, it will not go past this point. If the last change was made in the swap of locations i and i + 1, the next pass will not look at any elements past location i.
a. Write this new version of bubble sort.
b. Write a short paragraph that explains exactly why this new version of bubble sort will work.
c. Does this new version of bubble sort change the worst-case analysis? Give an analysis or justification for your answer.
d. This new version of bubble sort does change the average-case analysis.
Give a detailed explanation of what is involved in calculating this new average-case analysis.
4. Another version of bubble sort alternates passes so that the odd passes are like the original, but the even passes move from the bottom of the array to the top. On the odd passes the larger elements move toward the bottom, and on the even passes the smaller elements move toward the top.
a. Write this new version of bubble sort.
b. Write a short paragraph that explains exactly why this new version of bubble sort will work.
c. Does this new version of bubble sort change the worst-case analysis? Give an analysis or justification for your answer.
d. This new version of bubble sort does change the average-case analysis.
Give a detailed explanation of what is involved in calculating this new average-case analysis.
5. A third version of bubble sort combines the ideas of Questions 1 and 2. This bubble sort moves back and forth through the array but adjusts its upper and lower range of the sort based on where the last changes were made.
a. Write this third version of bubble sort.
3.2.4 EXERCISES
■
b. Write a short paragraph that explains exactly why this third version of bubble sort will work.
c. Does this third version of bubble sort change the worst-case analysis?
Give an analysis or justification for your answer.
d. This third version of bubble sort does change the average-case analysis.
Give a detailed explanation of what is involved in calculating this new average-case analysis.
6. Develop a formal argument that proves that the largest element must be in the correct place after the first pass of the BubbleSort loop.
7. Develop a formal argument that proves that if there are no swaps done on any pass of BubbleSort, the list must now be in the correct order.