Hi Minal,
First of all let me congratulate you for taking initiative to answer
questions. Question no. 3 is a teaser. In a way you have got the
essence of the question. But this is my argument and explanation. I
would also like others to argue and see if there are any holes in my
justification.
The best or the worst performance of the algorithm depends on the data
that it encounters. For the original Insertion sort algorithm- the
best performance (which is O(n)) will happen when it encounters
already sorted data (in ascending order in this case). This includes
the case when all the elements are the same. But when you make
modifications and replace the statement while(j > 0)and(A[j] > key)
with while(j > 0)and(A[j] >= key) , the best performance will not
happen in the case when all elements are equal. In fact worse
performance will happen in this case (which is O(n^2)) because it will
unnecessarily try and sort the data. But in case all elements are
distinct in ascending order, the best performance (which is O(n)) will
take place. The first algorithm is superior because it not doing any
redundant work. But as far as the best performance and the worst
performance is concerned, it will not change. I do not want to make
any assumption about the dataset. Therefore the answer is a).
Amit I have included your name in the group so that you can play the
role of a refree. Do you think my justification is correct? I am
inviting comments from all.
Minal, your answers to Q4 and Q5 are wrong. Anybody would like to help
Minal?
Venky