Quick Sort Vs Merge Sort
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Sorting algorithms are fundamental to computer science as they are used in various applications. Two of the most commonly used sorting algorithms are Quick Sort and Merge Sort. Both have their strengths and weaknesses and choosing between them often depends on the specific use case or environment constraints. This article provides an in-depth comparison of these two algorithms, exploring their mechanics, time complexity, space complexity, and use cases.
Quick Sort
Quick Sort is a highly efficient sorting algorithm and is often faster in practice than other algorithms, such as Merge Sort. It employs a divide-and-conquer strategy to sort elements and is known for its performance on average.
How Quick Sort Works
- Divide: Choose a pivot element from the array.
- Partitioning: Rearrange the elements such that elements less than the pivot come before it, and elements greater than the pivot come after it.
- Conquer: Recursively apply the above steps to the sub-arrays of elements with smaller and larger values.
Example of Quick Sort
Suppose we have an unsorted array: [3, 6, 8, 10, 1, 2, 1]. Here’s a step-by-step description of how Quick Sort would sort this array:
- Select a pivot:
2. - Partition the array into sub-arrays:
[1, 1](left of pivot),[2](pivot),[6, 8, 10, 3](right of pivot). - Recursively sort the sub-arrays.
Technical Points
- Time Complexity: Average: , Worst Case: (occurs when the smallest or largest element is always chosen as the pivot)
- Space Complexity: In-place, due to recursive stack
Merge Sort
Merge Sort is also a divide-and-conquer algorithm and it is known for its guaranteed time complexity for all input cases, making it highly predictable.
How Merge Sort Works
- Divide: Divide the unsorted array into two roughly equal sub-arrays.
- Conquer: Recursively sort the two sub-arrays.
- Combine: Merge the two sorted sub-arrays to produce the final sorted array.
Example of Merge Sort
For the same array [3, 6, 8, 10, 1, 2, 1], Merge Sort would perform the following:
- Split the array into
[3, 6, 8]and[10, 1, 2, 1]. - Further split into
[3],[6, 8],[10],[1, 2, 1]. - Continue splitting and then start merging back up:
- Merge
[6]and[8]to form[6, 8]. - Merge
[1],[2], and[1]to form[1, 1, 2]. - Continue merging until the entire array is sorted.
Technical Points
- Time Complexity: Always
- Space Complexity: , primarily due to the additional space required for the merging process
Comparison Table
Here's a summary of the Quick Sort and Merge Sort comparison:
| Feature | Quick Sort | Merge Sort |
| Time Complexity | Average: Worst: | Always: |
| Space Complexity | In-place: | |
| Stability | No | Yes |
| Divide & Conquer | Yes | Yes |
| Recursive Procedure | Yes | Yes |
| Use Cases | In-place sorting needed When average performance is critical | Linked list sorting Stable sorting required |
Additional Considerations
- Stability: Merge Sort is a stable sorting algorithm, meaning that it maintains the relative order of equal elements, whereas Quick Sort is not stable. This is particularly important when sorting records with multiple fields (e.g., sorting a list of records by one field while preserving the order by another field).
- In-place Sorting: Quick Sort is usually implemented as an in-place sort, meaning it requires only a small, constant amount of stack space in addition to the original array. In contrast, Merge Sort requires additional space proportional to the size of the array.
- Recursive Depth: The recursive depth for Quick Sort can become a bottleneck if a poor pivot is consistently chosen, leading to imbalanced partitions. On the other hand, Merge Sort splits the array more evenly, maintaining a lower maximum depth of recursion.
- Parallelism: Merge Sort is more naturally suited for parallel and distributed computing because merging sorted arrays can effectively be done in parallel, taking full advantage of modern multi-core processors.
Conclusion
Choosing between Quick Sort and Merge Sort depends on the particular requirements and constraints of the application. Quick Sort is often faster on average and more space-efficient, but its performance can degrade with poorly selected pivots. On the other hand, Merge Sort provides a more consistent time complexity and is stable, making it preferable for linked lists and when stability is crucial. Understanding the strengths and weaknesses of each algorithm aids in selecting the most efficient sorting strategy for a given problem.
Related reading
- Quick sort Worst case
- Quickest way to find missing number in an array of numbers
- Quickly checking if set is superset of stored sets
- Quickselect Algorithm - Simplified Explanation
- RAFT term condition to commit an entry
- Re-doing a reverted merge in Git
- QuickSelect Algorithm Understanding
- QuickSelect with Hoare partition scheme

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.