Mergesort is a recursivesorting algorithm. The core idea is that in each step, it sorts a sub-array by recursing from smaller sub-arrays half of its size, then merging them together.
It has a time complexity of O(nlogn). Each recursive step takes O(logn) levels (because we halve the sort set), and processing each level takes O(n) time.
Procedure
Mergesort operates on a subarray [a,⋯,b] as follows:
If a=b, then we don’t do anything, because the sub-array’s already sorted.
Compute the position of the middle element: k=⌊(a+b)/2⌋.
Recursively sort the subarray [a,⋯,k], then the subarray [k+1,⋯,b].
Merge the sorted sub-arrays into a combined sorted sub-array [a⋯b].