Question: 3. (25 marks) Consider the obvious algorithm for checking whether a list of integers is sorted (with respect to the usual order): start at the

3. (25 marks) Consider the obvious algorithm for checking whether a list of integers is sorted (with respect to the usual order): start at the beginning of the list, and scan along until we first find a successive pair of elements that is out of order. In that case, return false. If no such pair is found by the time we reach the end of the list, return true. Our elementary operation is a comparison between two integers.

(a) What is the worst case running time of this algorithm on an input list containing n elements? Write all 5-element input lists which contain the numbers 1, 2, 3, 4, 5 and yield the worst case.

(b) What is the best case running time of this algorithm on an input list with n elements? How many input lists are there which have size n, all elements being different, and yield the best case?

(c) Suppose that the input list is a random permutation of n distinct data items, and all such permutations are equally likely. Derive the average-case expected running time. Give both an exact and asymptotic answer.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!