Homework 18


Due : 9.30am, 7 August

Exercise 3 from Chapter 9 of the textbook (p.434)

Solution

Consider the following trace of numbers of comparisons generated by the algorithm for different values of J:
Jfor statementwhile statementarray element comparison
0121
1132
2143
3154
....
N-21NN-1
N-11N+1N
in general:1J+2J+1
Plus, there is one comparison for the outer loop when the algorithm terminates by the for statement failing. So, the total number of comparisons is:
Last updated : 7 August 2000 8.56pm