Merge Sort
Merge Sort is a Divide-and-Conquer sorting algorithm, which sorts a list recursively by dividing it into halves until a single --element or empty list is created; it then merges these sublists of create a software list.
Merge Sort is one of the faster and most stable sorting algorithms, with an time complexity in the average and worst case.
Algorithm
Simple step-by-step example
Given array: , a=1 and b=5 for the initial call.
- After the first recursive call, we would have sorted the first half (a=1, m=3), so the array would become
- After the 2nd recursive call, we would have sorted the 2nd half (a=4, b=5), so the array would become
- Then the final merge would merge both sides, resulting in
Visualisation