Quick sort is an internal algorithm which is based on divide and conquer strategy. In this:
- The array of elements is divided into parts repeatedly until it is not possible to divide it further.
- It is also known as “partition exchange sort”.
- It uses a key element (pivot) for partitioning the elements.
- One left partition contains all those elements that are smaller than the pivot and one right partition contains all those elements which are greater than the key element.
Merge sort is an external algorithm and based on divide and conquer strategy. In this:
- The elements are split into two sub-arrays (n/2) again and again until only one element is left.
- Merge sort uses additional storage for sorting the auxiliary array.
- Merge sort uses three arrays where two are used for storing each half, and the third external one is used to store the final sorted list by merging other two and each array is then sorted recursively.
- At last, the all sub arrays are merged to make it ‘n’ element size of the array.
Quick Sort vs Merge Sort
- Partition of elements in the array :
In the merge sort, the array is parted into just 2 halves (i.e. n/2).
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.
- Worst case complexity :
The worst case complexity of quick sort is O(n2) as there is need of lot of comparisons in the worst condition.
In merge sort, worst case and average case has same complexities O(n log n).
- Usage with datasets :
Merge sort can work well on any type of data sets irrespective of its size (either large or small).
The quick sort cannot work well with large datasets.
- Additional storage space requirement :
Merge sort is not in place because it requires additional memory space to store the auxiliary arrays.
The quick sort is in place as it doesn’t require any additional storage.
- Efficiency :
Merge sort is more efficient and works faster than quick sort in case of larger array size or datasets.
Quick sort is more efficient and works faster than merge sort in case of smaller array size or datasets.
- Sorting method :
The quick sort is internal sorting method where the data is sorted in main memory.
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.
- 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.
Quick sort is unstable in this scenario. But it can be made stable using some changes in code.
- Preferred for :
Quick sort is preferred for arrays.
Merge sort is preferred for linked lists.
- Locality of reference :
Quicksort exhibits good cache locality and this makes quicksort faster than merge sort (in many cases like in virtual memory environment).
|Basis for comparison||Quick Sort||Merge Sort|
The partition of elements in the array
|The splitting of a array of elements is in any ratio, not necessarily divided into half.||The splitting of a array of elements is in any ratio, not necessarily divided into half.|
Worst case complexity
Works well on
|It works well on smaller array||It operates fine on any size of array|
Speed of execution
|It work faster than other sorting algorithms for small data set like Selection sort etc||It has a consistent speed on any size of data|
Additional storage space requirement
|Inefficient for larger arrays||More efficient|
|for Arrays||for Linked Lists|
Locality of reference
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.
- Why Quick Sort preferred for Arrays and Merge Sort for Linked Lists?
- Merge Sort with O(1) extra space merge and O(n lg n) time
- Iterative Quick Sort
- C Program for Iterative Quick Sort
- Java Program for Iterative Quick Sort
- p5.js | Quick Sort
- Advanced Quick Sort (Hybrid Algorithm)
- Sorting Algorithm Visualization : Quick Sort
- Quick Sort using Multi-threading
- Visualization of Quick sort using Matplotlib
- 3D Visualisation of Quick Sort using Matplotlib in Python
- Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
- Merge Sort vs. Insertion Sort
- Median of an unsorted array using Quick Select Algorithm
- Count Inversions in an array | Set 1 (Using Merge Sort)
- Merge Sort for Linked Lists
- Merge Sort for Doubly Linked List
- Concurrent Merge Sort in Shared Memory
- 3-way Merge Sort
- Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : VIJESH1996