Mergesort with Python
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction to Mergesort
Mergesort is a classic sorting algorithm utilizing the divide-and-conquer paradigm. It was invented by John von Neumann in 1945 and has since become one of the most reliable and efficient sorting methods. Key features of Mergesort include its efficiency, stability, and suitability for large data sets.
How Mergesort Works
Mergesort follows a recursive strategy to sort an array:
- Divide: The list is divided into two approximately equal halves.
- Conquer: Each half is recursively sorted.
- Combine: The sorted halves are merged to produce a single sorted list.
This can be visually represented as a tree, where the original array is split until individual elements remain, and then those elements are merged together in a sorted manner.
Example of Mergesort
Consider the following unsorted array: [5, 2, 4, 6, 1, 3, 2, 6]
.
The process is as follows:
- Split into
[5, 2, 4, 6]and[1, 3, 2, 6]. - Further split into
[5, 2],[4, 6],[1, 3], and[2, 6]. - Continue splitting until you reach singleton arrays:
[5],[2],[4],[6],[1],[3],[2],[6]. - Start merging:
[2, 5],[4, 6],[1, 3],[2, 6]. - Intermediate merge:
[2, 4, 5, 6],[1, 2, 3, 6]. - Final merge:
[1, 2, 2, 3, 4, 5, 6, 6].
Python Implementation
Here is a Python implementation of Mergesort:
- Recursive Nature: Mergesort breaks down the array until each part is a single element.
- Merging Algorithm: The merging process plays a crucial role, ensuring the sorted order by comparing elements one by one from each half.
- Base Case and Recursive Calls: The recursion halts when the array length is 1 or 0, as these are inherently sorted.
- Time Complexity:
- Best Case:
- Average Case:
- Worst Case:
- Space Complexity: due to the additional space required for the temporary arrays for merging.
- Stability: Mergesort maintains the relative order of duplicate values, which can be beneficial in certain applications.
- Non-In-Place: Typically requires additional space for merging, making it not in-place.
- Parallelism: Mergesort inherently supports parallelization due to the independent nature of sorting halves.
- Sorting Large Lists: Handling large data efficiently due to its predictable time complexity.
- External Sorting: Used in external sorting algorithms where data doesn't fit in memory (e.g., database and file systems).
- Parallel Processing: Its divide-and-conquer nature allows for effective parallelization.
Related reading
- Merging 2 Binary Search Trees
- Merging a list of time-range tuples that have overlapping time-ranges
- Merging Ranges In C
- Merging two sorted linked lists
- Merging Tree Models from two random forest models into one random forest model at H2O in R
- Merging two arrays in .NET
- Messagebox with input field
- Method to uniformly randomly populate a disk with points in python

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.