Algorithms and Data Structures · Senior · Question 30 of 75

Why can an O(n log n) algorithm lose to an O(n²) one on real inputs?

Explanation

Insertion sort beats merge sort below a few dozen elements thanks to tiny overhead and cache-friendly access. That's why Timsort and introsort switch to insertion sort for small runs.

Further reading

Problem with a question

Algorithms and Data Structures · Senior · question 30

Test yourself

Take the Algorithms and Data Structures · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz