-2

Why not insertion sort with binary insert is considered to be best ?

Merge sort : T(n) = n + 2*T(n/2) = O(n*log(n))  
But insertion sort with binary insert : T(n) = log(n-1) + T(n-1) = O(log(n!))  
and n^n > n! ; so n*log(n) > log(n!)

for bigger n, it would really help to improve performance.

Or am I missing something ?

Forgive me if I am asking too trivial question, I am new to programming and I just want to get the facts right.

rnbguy
  • 1,369
  • 1
  • 10
  • 28
  • 2
    Insertion sort with binary insert still requires n inserts; I'm not sure how you've derived O(log(n!))? See wikipedia: http://en.wikipedia.org/wiki/Insertion_sort (Binary Insertion Sort). Worst case complexity is O(n*log(n)) – davmac Jul 19 '13 at 12:31
  • 1
    @HenkHolterman, no, just plain wrong. Eg 3^3 = 3 * 3 * 3, whereas 3! = 3 * 2 * 1. – davmac Jul 19 '13 at 12:35
  • Merge sort can be emberrassingly parallel. Good for gpu. Good for opetrons and xeons. – huseyin tugrul buyukisik Jul 19 '13 at 12:36
  • 1
    @HenkHolterman, take your own advice please. Eg 6^6 = 46656, 6! = 720. As n grows the difference between n^n and n! will increase. – davmac Jul 19 '13 at 12:41
  • You're right, I was reading n^2, need more coffee. – H H Jul 19 '13 at 12:42
  • @davmac ya right. I forgot about the inserting part still takes O(n) complexity. thank you ! this answers my question. I upvoted everyone who gave THIS reason. – rnbguy Jul 19 '13 at 12:42
  • Although n^n>n!, once you take logarithms, they are equal within a constant multiple. So O(n log n)=O(n!). – Teepeemm Jul 19 '13 at 20:09
  • @Teepeemm umm. can you give some links in favour of what you have said. because it looks like `n! ∈ o(nlogn)` check [this plot](http://www.wolframalpha.com/input/?i=plot+n*log%28n%29-log%28n%21%29) – rnbguy Jul 19 '13 at 21:23
  • 1
    Since we're working with big O, we would want to look at the ratio of the functions (which looks to be decreasing to 1.25), not their difference. To give a rough proof, n!>(n/2)^(n/2), so log(n!)>n/2 log(n/2), and n log n ∈ O(log n!). (I should also add somewhere that I mistakenly wrote O(n!) in my previous comment.) To get a more exact proof (and find a limiting value of the plot), you can use [Stirling's approximation](http://en.wikipedia.org/wiki/Stirling's_approximation), and the first displayed formula at Wikipedia shows that the ratio of the functions tends toward 1. – Teepeemm Jul 20 '13 at 18:38

2 Answers2

3

I think your estimation of insertion sort's complexity is wrong. You didn't describe the details of how you got the result, but it seems you forgot about the time needed to insert - I mean the time you need to move some part of the sorted array to make place for the element you are inserting.

After you sorted n-1 elements, you need O(log(n)) time to find the place for n-th element and O(n) (pessimistically) time to move part of the sorted array and make place for the n-th element. So the total complexity is

O(1 + ... + n + log 1 + ... + log n) = O(n^2 + n log n) = O(n^2).

You don't improve your algorithm by binary search, because you have to shift part of array (of linear size in terms of n) anyway.

Bartosz Marcinkowski
  • 6,651
  • 4
  • 39
  • 69
1

The insertion sort with binary insert has a running time of O(n^2) on an average. Try exploring the wiki page here. Also, check out this SO post.

Community
  • 1
  • 1
Sankalp
  • 2,796
  • 3
  • 30
  • 43