Divide and Conquer Sorting Algorithms
Divide and Conquer Sorting Algorithms
STUDY GUIDE
DIVIDE-AND-CONQUER SORTING
ALGORITHMS
The Divide-and-Conquer Method
Two algorithms use the "divide-and-conquer" method: merge sort and quick sort.
Merge Sort
Merge sort takes an array and splits it in half over and over again until it’s small and sorted, and then it merges those small sorted pieces back
together in order. Merge sort is actually two algorithms, the merge sort algorithm and the merge algorithm. Each algorithm handles a
different phase of the merge sort process:
1. Divide: The merge sort algorithm splits the array in half until it can’t anymore.
2. Conquer: The merge algorithm puts the split pieces back together.
The merge sort algorithm divides arrays until they only have one item in them (which are fundamentally sorted). Like any recursive function,
the merge sort algorithm is defined by a base case and a recursive case:
The merge algorithm takes two sorted arrays, compares them, and sorts them into a results list. It keeps doing this until it creates a large,
sorted array. Here is the basic process of the merge algorithm. It starts with the sorted single-element arrays created by the merge sort
algorithm:
Space complexity: The merge algorithm creates a separate "results array" as the data is being merged, so it's an out-of-place sort and uses
O(N) space complexity.
Time complexity: The two components of merge sort have two different time complexities.
• The recursive merge sort algorithm takes O(log(N)) time, which is very efficient.
• The non-recursive merge algorithm is an O(N) operation.
• To get the overall complexity of merge sort, we multiplyO(log(N)) and O(N) to get O(N log(N))
Quick Sort
Quick sort is another recursive divide-and-conquer sort. Quick sort differs from merge sort in how it approaches the divide phase: merge sort
divides an array into two parts at a time, but quick sort divides an array into three parts at a time:
Elements are sorted by recursively calling quick sort on the array on either side of the pivot.
The concept of quick sort and partitioning is complicated. Here are a couple of fun resources you can explore if you want to dive in further:
• This tool lets you move through the algorithm on a sample data set step-by-step.
• This video helps you visualize quick sort with folk dancing.
Space complexity: Quick sort's space complexity isO(log(N)), which is much lower than merge sort's.
Time complexity: Quick sort’s worst-case time efficiency isO(N^2), but its average-time complexity is Θ(N log(N)), which is the same as merge
sort.
Merge sort:
Use this tool to compare merge and quick sort (and other sorting algorithms).