Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

GP's description is selection sort, not insertion sort, which still requires extra scanning instead of manipulation and remains O(n^2), even in real life.


> remains O(n^2), even in real life.

This is something of a grandiose claim. How are you thinking of a physical insertion sort working? If you don't have a model, you can't say anything about the time requirements.

But note that if we conceptualize insertion sort like this:

    loop:
      pick up a book
      find the place within the sorted books where this book belongs
      open a space for the book
      insert the book
the four steps in that model are O(1), O(log n), O(1), and O(1), and the loop repeats n times, so we have an upper bound of O(n log n).

The reason insertion sort is O(n^2) when operating on an array is that step 3, "open a space for the book in our hand" is O(n) in that case, because we can only move one book at a time.


I was talking selection sort, not insertion sort.

But, while we're here... Insertion sort is O(n^2) on the number of comparison operations, because this step is O(n) on the number of sorted items (you start with 0 of them, and end with n of them, for an average of n/2, which is O(n)):

> find the place within the sorted books where this book belongs

You do this once for each unsorted item (you start with n of them, and end with 0 of them, for an average of n/2, which is O(n)).

Granted, in real life, your brain does a better job of remembering roughly the correct place for each book, so I would say average case with a small number of books you are correct. If you're sorting a large number of books, you need to find the correct spot in less than O(n) comparisons to do better than O(n^2) on the algorithm. I think finding the correct spot would still be O(n), just with a small constant.

Interestingly, if you were sorting a massive volume of books like this and you started to forget the right spot for things, you might modify your strategy and start binary searching for it. This would help you find the spot in O(log(n)), and you'd be doing the sort in O(n*log(n))! It's a small improvement that yields binary insertion sort.


> Insertion sort is O(n^2) on the number of comparison operations, because this step is O(n) on the number of sorted items

Locating the correct position in the sorted items is O(log n) on the number of sorted items. You point this out yourself later in your comment. Because the sorted items are sorted, it's not necessary to examine each of them.

Doing O(log i) work as i varies from 1 to n is O(log n!) work, which is O(n log n); not much different from doing O(log n) work as i varies from 1 to n.

I'm not sure how to interpret the two halves of this quote. It looks to me like I'm doing O(n log n) comparisons, and also finishing the sort in O(n log n) work overall. I'm not taking O(n^2 log n log n) work to finish the sort.


> Because the sorted items are sorted, it's not necessary to examine each of them.

This is true, but I'm being pedantic and calling the quadratic form of insertion sort just "insertion sort", and the form that does a binary search on the sorted items "binary insertion sort".

https://en.wikipedia.org/wiki/Binary_insertion_sort


Please stop arguing, you're agreeing with both of us.

I only replied originally because I thought you misread the original comment.


Ah, my error. If you want to see what I was thinking, imagine I'd pulled the "fuller" quote 'insertion sort, which still requires extra scanning instead of manipulation and remains O(n^2), even in real life'.

The point of my comment was just that the complexity of an algorithm as analyzed under one set of primitive operations doesn't automatically translate into another set of operations, even when at a high level it's the same algorithm. It is dangerous to study CS, learn that selection sort (on arrays, on a computer well described by a C-like language) is O(n^2), and then conclude that selection sort (in any context) is inherently O(n^2). That may be true of selection sort in specific -- it's difficult to avoid concluding that the selection step is roughly ϴ(n), and must always run n times -- but the reasoning is faulty, and won't transfer to other algorithms.


This makes more sense now. The full relevant part of my comment:

> GP's description is selection sort, not insertion sort, which still requires

The double-comma construction is meant to be read as an aside. Probably would've been clearer using parens:

> GP's description is selection sort (not insertion sort), which still requires

I often use the commas though because I'm mentally verbalizing what I type and commas have a pause that makes the aside work even aloud, while it's not clear how you'd verbalize parens.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: