1

Watching the free MIT Algorithms course on iTunesU and Im stuck on the first lecture.

Take an insertion sort, its time is really T(n/2) in worst-case (reversed order array/list), but they say that this is theta n squared. I would thought this would be theta n. Im lost how they say this is n squared. Im stuck how they jump to concluding this is n squared, Wikipedia is not helping either. Can someone dumb it down further?

2 Answers 2

4

Insertion-sorting an array of 4 elements that starts in reverse order:

4 3 2 1

first, insert the "4" into its proper position in an array of length 1 (i.e. do nothing).

next, insert the "3" into its proper position in an array of length 2:

3 4 2 1

(we had to move the 3 and the 4)

next, insert the "2" into its proper position in an array of length 3:

2 3 4 1

(we had to move the 2, the 3 and the 4)

next, insert the "1"

1 2 3 4

(we had to move the 1, the 2, the 3 and the 4)

We performed n steps, and each step k required moving k elements (or k-1 swaps, depending how you want to look at it). The sum of k from 1 to n is Theta(n^2).

In the case of a simple linked list structure[*], we can move an object into its proper place in O(1), but in general finding the proper place still requires a linear search through the part of the data that's already sorted, so it still ends up only O(n^2) for general input. A basic insertion sort for a linked list happens to deal very well with reverse-ordered data, though, because it happens to always find the correct insertion position immediately. So we get n steps of O(1) each for a total O(n) running time for this particular data.

Assuming we still choose the first unsorted element to insert, and that we search forward through the sorted part of the list at each step, then the worst case of insertion sort for a list is already-sorted data, and is Theta(n^2) again.

[*] meaning, nothing fancy like a skip list.

Sign up to request clarification or add additional context in comments.

Comments

1

From wikipedia:

The worst case input is an array sorted in reverse order. In this case every iteration of the inner loop will scan and shift the entire sorted subsection of the array before inserting the next element. For this case insertion sort has a quadratic running time (i.e., O(n2)).

First loop is iterating on the array/list to sort, inner loop iterates on the partially sorted array/list. If it's already sorted you can see that you iterate all the way to the end of the sorted container every time.

Here's more explanation in pseudo:

for element in unsorted_container
    for current_element in sorted_container
        if element < current_element -> Will never happen since sorted in reverse order.
            InsertBefore(element, current_element)
    if element not inserted
        InsertAtEnd(element) <- Will always execute this part since it will always insert at end.

5 Comments

Its not really n squared though or anywhere close. Its it only n cost on last iteration where j=n (j being the loop position). Im missing something here. So thinking out loads, its really on say 5 item array. (1) + (2) + (3) + (4) + (5) but does equals O(n2)?
@mattcodes: 1 + 2 + ... + n = n*(n+1)/2, which is indeed Theta(n^2).
@mattcodes to elaborate on the correct remarks from Eric and Steve, even if your operations are only (n^2)/2, the N^2 part of that expression will make itself blatantly known when you double or quadrupal N. If you are working on a problem with this time complexity and N = 100000000000 and 1 millisecond to complete each operation, you won't be alive at the time when the algorithm terminates correctly even when the complexity is (n^2)/2, and not (n^2).
And anyway, complexity ignores constant multipliers like (1/2). If the running time is exactly (n^2)/2, then the time complexity is n^2.
Right. I think I've got it. In insertion sort its (n-1) * (n/2) where we have index start/count at 1. Since its multiplication of something involving n, its not constant, and so in theta is n squared. Woohoo. Thanks San & Steve

Your Answer

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