[Sorting Algorithm] QuickSort vs. MergeSort

Quick sort 對決 Merge sort
Conclusion

In general, swap operations and space allocation operations consume more time than comparison operations.

When comparison operations are not time-consuming, choosing QuickSort can effectively reduce the time.

When comparison operations are time-consuming, choosing MergeSort can effectively reduce comparison time.

This is also why in Java, the Arrays.sort() method uses the Dual-Pivot Quicksort algorithm for primitive data types (such as int, long, byte, char, etc.). For object types (like Integer, String, etc.), it uses the TimSort algorithm (Merge Sort + Insertion Sort).

Let me illustrate this conclusion with a practical example.

Why is QuickSort so popular?

The popularity and widespread use of QuickSort are due to several significant advantages that make it very effective in many scenarios. Here are the main reasons why QuickSort is favored:

  1. Efficient Average Performance
  • QuickSort has an average time complexity of O(n log n), making it highly efficient in most cases. For sorting tasks involving large amounts of data, QuickSort can provide good performance.
  1. In-place Sorting
  • QuickSort is an in-place sorting algorithm, meaning it doesn’t require additional significant storage space to sort an array. This in-place sorting characteristic makes QuickSort more space-efficient compared to sorting algorithms that need extra storage space, like merge sort.
  1. Cache-Friendly
  • QuickSort optimizes cache usage by recursively partitioning within the array, which helps because it frequently accesses nearby data. Good cache utilization can significantly speed up sorting.
Is there a sorting method faster than QuickSort?

There is no “best” sorting method in the world, only the most suitable one. If comparison operations are particularly time-consuming, merge sort can outperform QuickSort. Here’s why merge sort is suitable for data sets where comparisons are costly:

1. Optimized Number of Comparisons

  • During the merging step, merge sort compares elements of two sorted subsets in sequence, meaning each element is compared at most once. Therefore, merge sort minimizes the number of comparisons, which is especially important when comparison operations are lengthy.

2. Stable Time Complexity

  • Regardless of the initial state of the data set, the time complexity of merge sort remains at O(n log n), providing predictability in performance. In contrast, the time complexity of QuickSort can degrade to O(n^2) in the worst-case scenarios (e.g., when the array is already sorted or all elements are equal), where QuickSort’s performance can significantly decrease if comparison operations are time-consuming.

3. Suitable for Large Data Sets

  • For large-scale data sets, especially when data cannot be entirely loaded into memory, merge sort can effectively perform external sorting. External sorting refers to data stored on external disks, with merge sort loading and sorting data in stages, making it suitable for such situations. In contexts where comparison operations are time-consuming, this advantage of merge sort is particularly important because it allows the system to process a large volume of data more efficiently.
Practical Comparison

Next, I’ll compare the sorting times and the number of comparisons between Quick Sort and Merge Sort.

Scenario 1: When comparison operations are not time-consuming, QuickSort performs better than Merge Sort, for the following reasons:

  1. If the comparison is merely about the size of values, the time for comparison operations will be far less than read/write operations, and QuickSort is an algorithm that can effectively reduce read/write operations.
  2. QuickSort does not require time to allocate space.

Scenario 2: When comparison operations are time-consuming, Merge Sort outperforms QuickSort because Merge Sort can effectively reduce the number of comparison operations.

Below is the table for the number of comparisons.

Data Size1000100001000001000000
Quick Sort10291153307203021524397321
Merge Sort8740120524153657918674946
Further Reading