Open In App

Quick Sort vs Merge Sort

Last Updated : 19 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Quick sort first partitions the array and then make two recursive calls. Merge sort first makes recursive calls for the two halves, and then merges the two sorted halves.

The following are differences between the two sorting algorithms.

  1. Partition of elements in the array : In the merge sort, the array is parted into just 2 halves (i.e. n/2). whereas In case of quick sort, the array is parted into any ratio. There is no compulsion of dividing the array of elements into equal parts in quick sort.
  2. Worst case complexity : The worst case complexity of quick sort is O(n^2) as there is need of lot of comparisons in the worst condition. whereas In merge sort, worst case and average case has same complexities O(n log n).
  3. Additional storage space requirement : Merge sort is not in-place because it requires additional memory space to store the auxiliary arrays. whereas The quick sort is in place as it doesn’t require any additional storage.
  4. Sorting method : The quick sort is internal sorting method where the data is sorted in main memory. whereas The merge sort is external sorting method in which the data that is to be sorted cannot be accommodated in the memory and needed auxiliary memory for sorting.
  5. Stability : Merge sort is stable as two elements with equal value appear in the same order in sorted output as they were in the input unsorted array. whereas Quick sort is not stable in this scenario. But it can be made stable using some changes in code.
  6. Preferred for : Quick sort is preferred for arrays. whereas Merge sort is preferred for linked lists. Quick Sort performs better in general, but Merge Sort works better for external sorting.
  7. Locality of reference : Quicksort exhibits good cache locality and this makes quicksort faster than merge sort (in many cases like in virtual memory environment).
  8. Tail Recursion : Quick Sort is tail recursive in nature and hence easily optimized by doing tail call elimination. While Merge Sort is not tail recursive.
Basis for comparisonQuick SortMerge Sort
The partition of elements in the arrayThe splitting of a array of elements is in any ratio, not necessarily divided into half.In the merge sort, the array is parted into just 2 halves (i.e. n/2).
Worst case complexityO(n^2)O(n log n)
Works well onIt works well on smaller arrayIt operates fine on any size of array
Speed of executionIt work faster than other sorting algorithms for small data set like Selection sort etcIt has a consistent speed on any size of data
Additional storage space requirementLess(In-place)More(not In-place)
EfficiencyInefficient for larger arraysMore efficient
Sorting methodInternalExternal
StabilityNot StableStable
Preferred forfor Arraysfor Linked Lists
   
Locality of referencegoodpoor
Major work The major work is to partition the array into two sub-arrays before sorting them recursively.Major work is to combine the two sub-arrays after sorting them recursively.
Division of array Division of an array into sub-arrays may or may not be balanced as the array is partitioned around the pivot.Division of an array into sub array is always balanced as it divides the array exactly at the middle.
Method Quick sort is in-  place sorting method.Merge sort is not in - place sorting method.
Space Quicksort does not require additional array space.For merging of sorted sub-arrays, it needs a  temporary array with the size equal to the number of input elements.

Next Article

Similar Reads

  翻译: