Question

1. Consider an array of n distinct values in which the first n − 1 values are sorted, and the las...

1. Consider an array of n distinct values in which the first n − 1 values are sorted, and the last element is not. (It could be smaller than the first element, larger than element n − 1, or anywhere in between.) Give the worst-case number of comparisons that Insertion Sort will perform in this scenario. You can give your answer in terms of big-Theta if you wish to ignore low-order terms and constants.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The subarrays from [1,i] for 2\le i\le n-1 are already sorted as per the hypothesis. So the I-th element must be compared with all previous (i-1) elements. This itself gives rise to

\sum_{i=2}^{n-1}(i-1)=\frac{(n-2)(n-1)}{2} which is Θ(n2)

The last insertion may take 1 comparison or n-1 comparisons depending on how large the last element is

In any case, the number of comparisons is Θ(n2)

Add a comment
Know the answer?
Add Answer to:
1. Consider an array of n distinct values in which the first n − 1 values are sorted, and the las...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT