We have discussed sorting weapons used by different languages in previous article. In this article, C++’s Sorting Weapon, Introsort is discussed.
What is Introsort?
Simply putting, it is the best sorting algorithm around. It is a hybrid sorting algorithm, which means that it uses more than one sorting algorithms as a routine.
How does it work?
Introsort begins with quicksort and if the recursion depth goes more than a particular limit it switches to Heapsort to avoid Quicksort’s worse case O(N2) time complexity. It also uses insertion sort when the number of elements to sort is quite less.
So first it creates a partition. Three cases arises from here.
- If the partition size is such that there is a possibility to exceed the maximum depth limit then the Introsort switches to Heapsort. We define the maximum depth limit as 2*log(N)
- If the partition size is too small then Quicksort decays to Insertion Sort. We define this cutoff as 16 (due to research). So if the partition size is less than 16 then we will do insertion sort.
- If the partition size is under the limit and not too small (i.e- between 16 and 2*log(N)), then it performs a simple quicksort.
Why is it better than simple Quicksort or Why the need of Introsort?
Since Quicksort can have a worse case O(N2) time complexity and it also increases the recursion stack space (O(log N) if tail recursion applied), so to avoid all these, we need to switch the algorithm from Quicksort to another if there is a chance of worse case. So Introsort solves this problem by switching to Heapsort.
Also due to larger constant factor, quicksort can perform even worse than O(N2) sorting algorithm when N is small enough. So it switches to insertion sort to decrease the running time of sorting.
Also if a bad pivot-selection is done then the quicksort does no better than the bubble-sort.
Why is Insertion Sort used (and not Bubble Sort, etc)?
Insertion sort offers following advantages.
- It is a known and established fact that insertion sort is the most optimal comparison-based sorting algorithm for small arrays.
- It has a good locality of reference
- It is an adaptive sorting algorithm, i.e- it outperforms all the other algorithms if the array elements are partially sorted.
Why is Heapsort used (and not Mergesort etc)?
This is solely because of memory requirements. Merge sort requires O(N) space whereas Heapsort is an in-place O(1) space algorithm.
Why is Heapsort not used in place of Quicksort when the partition size is under the limit ?
This question is same as why Quicksort generally outperforms Heapsort ?
The answer is, although Heapsort also being O(N log N) in average as well as worse case and O(1) space also, we still don’t use it when the partition size is under the limit because the extra hidden constant factor in Heapsort is quite larger than that of Quicksort.
Why is cut-off 16 for switching from quick sort to insertion sort, and 2*logN for switching from quick sort to heap sort ?
These values are chosen empirically as an approximate because of various tests and researches conducted.
-313 -9 -9 1 3 23 23 32 233
Is Introsort stable ?
Since Quicksort is also not stable so Introsort is also not stable.
Best Case – O(N log N)
Average Case- O(N log N)
Worse Case- O(N log N)
where, N = number of elements to be sorted.
Just like quicksort, it may use O(log N) auxiliary recursion stack space.
This article is contributed by Rachit Belwariar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.