Why does the Arrays.sort(Object[]) use insertion sort if the array length is less than 7?
-
Arrays.sort(Object[]) applies insertion sort if the array length < 7, else it applies merge sort. Why would it do so if merge sort has O(n log n) time complexity and insertion sort has O(n^2)?
-
Answer:
The thing to remember about the big-O notation is that it omits (among other things) a constant. The running time is proportional to the value in the O, not equal to it. So the question is, is C_1O(n^2) greater or less than C_2O(n\, log\,n)? The answer, of course, depends on C_1 and C_2. For small values of n, the value of the constants dominates the equation. In a trivial case like this, remember that insertion sort is really O(1/2 n^2). For n=6, that's 18. For n log n, that's 15.5, essentially the same. For n=3, it actually crosses over: 4.7 for the merge sort, 4.5 for the insertion sort. That's before we get into implementation details: nonrecursive sorts like insertion have lower overhead.
Joshua Engel at Quora Visit the source
Other answers
Because asymptotic analysis is part of the story, but you need to consider other factors as well: MergeSort copies at least the references to the two underlying arrays. This takes time, and is part of the constant factor hidden in the analysis. For small arrays, it is significant. InsertionSort's quadratic worst case is for an initially reverse order of items, which can happen, but not all the time. When the array is already near-sorted, InsertionSort is one of the most efficient algorithms. This saves some recursive calls You can find more details at Javamex's "Performance of the Java Sorting Algorithm" (http://www.javamex.com/tutorials/collections/sorting_java_algorithm_performance.shtml)
Yuval Feinstein
The answer is in the question itself. Asymptotic analysis is relative comparison, not an absolute measurement. For better understanding, draw graphs of lineararithmetic O(N log N) and quadratic O(N^2) complexities, compare these graphs before 7, and after 7. Generally, while sorting large data these linear arithmetic algorithms and insertion sort are combined to take the advantage that, insertion sort performs better on almost sorted data.
Venkateswara Rao Sanaka
Related Q & A:
- How to get Javascript array length?Best solution by Stack Overflow
- Why do 7-Eleven use lowercase letter "n" for the logo?Best solution by answers.yahoo.com
- Why is RoadRunner so difficult to use?Best solution by support.moates.net
- Why is it so hard to use PayPal?Best solution by Yahoo! Answers
- Why does the green "Do you use the Yahoo! Toolbar on multiple computers?" keep coming up?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.