Merge-sort
Understand the efficiency and effectiveness of the merge-sort algorithm.
We'll cover the following...
Merge-sort algorithm and its recursive structure
Merge-sort is one of the earliest algorithms designed for general-purpose stored-program computers. The algorithm was developed by John von Neumann in 1945 and described by him in detail in a publication with Herman Goldstine in 1947 as one of the first non-numerical programs for the EDVAC. It works as follows:
- Divide the input array into two subarrays of roughly equal size.
- Recursively merge-sort each of the subarrays.
- Merge the newly-sorted subarrays into a single sorted array.
The first step is completely trivial—just divide the array size by two—and we can delegate the second step to the Recursion Fairy. All the real work is done in the final merge step. A complete description of the algorithm is given in the above illustration. To keep the recursive structure clear, we’ve extracted the merge step into an independent subroutine. The merge algorithm is also recursive—identify the first element of the output array, and then recursively merge the rest of the input arrays.
Algorithm
...
Create a free account to access the full course.
By signing up, you agree to Educative's Terms of Service and Privacy Policy