9
$\begingroup$

I tried this problem from CLRS (Page 39, 2.3-4)

We can express insertion sort as a recursive procedure as follows. In order to sort A[1... n], we recursively sort A[1... n-1] and then insert A[n] into the sorted array A[1... n-1]. Write a recurrence for the running time of this recursive version of insertion sort.

The recurrence I formed was

$$ T(n) = \begin{cases}\Theta(1) & \textrm{if } n = 1,\\ T(n-1) + \Theta(n) & \textrm{if } n > 1. \end{cases} $$

My reasoning

  • the base case of $n = 1$ the list is sorted so there is no work hence constant time.
  • For all other cases the time depends on sorting the sequence A[1...n-1] and then insertion into that sequence. Hence it should be their sum, i.e., $T(n-1) + \Theta(n)$.

I wanted to know whether the recurrence relation is correct. If not what are the mistakes and how to correctly formulate a recurrence relation?

Raphael
73.3k31 gold badges184 silver badges406 bronze badges
asked Jul 9, 2013 at 6:51
$\endgroup$
1
  • 1
    $\begingroup$ You may be interested in our reference questions. In particular, the notion of "runtime" is fuzzy, the recurrence with $\Theta$-terms is not the nicest way of putting things and several kinds of solving recurrences have been discussed. Note that "yes-no"-questions are generally undesired here. (I note that the question is old; leaving the comment for reference.) $\endgroup$ Commented Sep 15, 2014 at 10:52

1 Answer 1

6
$\begingroup$

According to the description you provided the recurrence is correct.
you might think it should be
T(n)=\begin{Bmatrix} 1 & ,\ n=1\\ T(n-1)\ +\ \Theta(log\ n) & ,\ otherwise \end{Bmatrix}
because you can find the insertion place by using Binary-Search, however in order to actually insert the element you'll have to move away all the elements in the worst case.

answered Jul 9, 2013 at 14:06
$\endgroup$
3
  • $\begingroup$ Searching linearly takes O(n) and by doing binary search it is O(log n). But isn't the worst-case the movement of all the elements which should take O(n) hence making the overall complexity O(n)? So why is the term in n != 1 is O(log n) instead of being O(n)? $\endgroup$ Commented Jul 9, 2013 at 15:10
  • 2
    $\begingroup$ you're right, that is exactly what i said. the above formula is wrong exactly because the movement of the elements takes O(n) $\endgroup$ Commented Jul 9, 2013 at 15:18
  • $\begingroup$ I should be reading more carefully. Thanks for answering. $\endgroup$ Commented Jul 9, 2013 at 15:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.