Merge Sort
Last updated
Last updated
A comparison-based algorithm that sorts a given dataset.
It is classified as a “divide and conquer” algorithm.
Divide: The algorithm begins by dividing the input array into two roughly equal-sized subarrays. This process continues recursively until the subarrays become small enough to be considered sorted individually (usually when they contain only one element).
Conquer: Once the subarrays are small enough, they are considered sorted by definition. If the subarray has more than one element, the algorithm recursively applies the merge sort algorithm to further divide it. This step ensures that every element in the array is individually sorted.
Merge: After all the recursive divisions and sorting are complete, the algorithm starts merging the sorted subarrays back together to produce a single sorted array. It compares the elements from each subarray and selects the smallest (or largest, depending on the sorting order) element to place in the final sorted array. The process continues until all the elements from the subarrays are merged.
Finalize: Once the merging process is complete, the array is fully sorted. The sorted array is the output of the merge sort algorithm.
Complexity of Merge Sort:
for all best, average and worst case scenarios.