Merge Sort in Python
Learn Merge Sort in Python! A fun, beginner-friendly guide to the Divide and Conquer sorting strategy with guaranteed fast performance.
Try it yourself
Run this code directly in your browser. Click "Open in full editor" to experiment further.
Click Run to see output
Or press Ctrl + Enter
How it works
Merge Sort is an amazingly smart sorting algorithm that uses a legendary strategy called Divide and Conquer!
How It Works
Imagine you have a giant, messy stack of 100 test papers to sort alphabetically. Sorting all 100 at once is totally overwhelming!
1. Divide: What if you just rip the stack in half? Now you have two stacks of 50. Still too big? Keep ripping them in half until you just have 100 individual papers! (Because a stack of exactly 1 paper is technically already sorted!)
2. Conquer (Merge): Now, look at two individual papers. Put them in order. Boom! You have a sorted stack of 2 papers! Take another sorted stack of 2, and merge them perfectly into a sorted stack of 4!
3. Keep stitching these perfectly sorted mini-stacks together until the whole giant stack is perfectly sorted.
Why is it so awesome?
5s) in your list, the one that was originally first will stay first! It respects the original order.Time and Space Complexity
When to Use
Related examples
Bubble Sort in Python
Learn the Bubble Sort algorithm in Python! Step-by-step visual explanation with beginner-friendly, optimized Python code.
Quick Sort in Python
Learn Quick Sort in Python! A beginner-friendly guide to the famous Divide and Conquer algorithm. See how pivots and partitioning make sorting blazing fast.
Binary Search in Python
Learn Binary Search in Python! A super fun and beginner-friendly guide to finding elements in a sorted array blazing fast using iterative and recursive code.