Merge Sort is a divide-and-conquer algorithm that was invented by John von Neumann in 1945. It is an efficient, stable sorting algorithm that works by recursively dividing the array into halves, sorting each half, and then merging the sorted halves back together. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, it has several advantages, including simplicity, efficiency for small datasets, and stability.
How Merge Sort Works:
- Divide the unsorted list into n sublists, each containing one element.
- Repeatedly merge sublists to produce new sorted sublists until there is only one sublist remaining.
- This final sublist is the sorted list.
- Repeat this process for all elements in the array until the entire array is sorted.
Complexity Analysis:
- Time Complexity: O(n log n) in the worst and average cases, O(n) in the best case (when the array is already sorted).
- Space Complexity: O(n) (not in-place sorting).
Characteristics:
- Merge Sort is stable.
- It is not an in-place sorting algorithm.
- Efficient for large datasets.
- The graph describing the Merge Sort time complexity looks like this:
