Merge Sort 🖇️
Back to algorithms list.Functionality
Merge Sort is a divide-and-conquer sorting algorithm that recursively splits the list into smaller sublists until each sublist contains only one element. It then merges these sublists back together in sorted order. The process involves two main steps:
- Divide: The list is divided into two halves repeatedly until each sublist contains a single element.
- Merge: Pairs of sublists are merged by comparing their elements and arranging them in order, resulting in a fully sorted list.
Advantages
- Efficiency: Merge Sort has a consistent time complexity of O(n log n) in the best, average, and worst cases, making it very efficient for large datasets.
- Stability: It is a stable sorting algorithm, preserving the relative order of equal elements.
- Predictable Performance: Its performance does not degrade significantly on particular types of data (e.g., partially sorted lists).
Disadvantages
- Additional Memory Usage: Merge Sort requires additional space proportional to the size of the input list (O(n)) because it uses temporary arrays for merging.
- Not In-place: Unlike simpler algorithms like Bubble Sort or Insertion Sort, Merge Sort does not sort the elements within the original array without extra memory.
Conclusion
Merge Sort is ideal for sorting large datasets where stability and guaranteed performance are crucial. However, its higher memory usage makes it less suitable for memory-constrained environments.
Source: Article about Merge Sort on wikipedia.org
Source code: github.com/danielnagel/algorithms