This a common question asked in DS interviews that despite of better worst case performance of merge sort, quicksort is considered better than mergesort. There are certain reasons due to which quicksort is better especially in case of arrays:
- Auxiliary Space : Mergesort uses extra space, quicksort requires little space and exhibits good cache locality. Quick sort is an in-place sorting algorithm. In-place sorting means no additional storage space is needed to perform sorting. Merge sort requires a temporary array to merge the sorted arrays and hence it is not in-place giving Quick sort the advantage of space.
- Worst Cases : The worst case of quicksort O(n2) can be avoided by using randomized quicksort. It can be easily avoided with high probability by choosing the right pivot. Obtaining an average case behavior by choosing right pivot element makes it improvise the performance and becoming as efficient as Merge sort.
- Locality of reference : Quicksort in particular exhibits good cache locality and this makes it faster than merge sort in many cases like in virtual memory environment.
- Merge sort is better for large data structures: Mergesort is a stable sort, unlike quicksort and heapsort, and can be easily adapted to operate on linked lists and very large lists stored on slow-to-access media such as disk storage or network attached storage. Refer this for details
The std::sort() function which is present in C++ STL is a hybrid sorting algorithm provides average and worst case time complexity of O(nlogn). The sorting algorithm which it uses is called Introsort.
Introsort is combination of both quicksort and heapsort, It begins with quicksort and switch to heapsort if recursion depth exceeds a level based on the number of elements being sorted.
Related Article: Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- How to make Mergesort to perform O(n) comparisons in best case?
- C++ Program for QuickSort
- Stable QuickSort
- Dual pivot Quicksort
- Java Program for QuickSort
- Python Program for QuickSort
- QuickSort using Random Pivoting
- When does the worst case of Quicksort occur?
- QuickSort on Doubly Linked List
- 3-Way QuickSort (Dutch National Flag)
- QuickSort on Singly Linked List
- Generic Implementation of QuickSort Algorithm in C
- Hoare's vs Lomuto partition scheme in QuickSort
- Comparisons involved in Modified Quicksort Using Merge Sort Tree