worst-case optimal stable divide and conquer comparison sorting algorithm
Wikipedia
en.wikipedia.org › wiki › Merge_sort
Merge sort - Wikipedia
2 weeks ago - In computer science, merge sort (also commonly spelled as mergesort or merge-sort) is an efficient and general purpose comparison-based sorting algorithm. Most implementations of merge sort are stable, which means that the relative order of equal elements is the same between the input and output.
GeeksforGeeks
geeksforgeeks.org › dsa › merge-sort
Merge Sort - GeeksforGeeks
The merge function of merge sort to efficiently solve the problems like union and intersection of two sorted arrays. ... Stability : Merge sort is a stable sorting algorithm, which means it maintains the relative order of equal elements in the input array.
Published October 3, 2025
Did anyone else have a hard time learning merge sort?
Well, let's break it down to something simpler. Let's just learn merge. Merge is a function that takes as input two already-sorted lists and returns as output a new list with all of the elements of the input lists, also sorted. For example, merge([2,4,6],[3,5]) returns [2,3,4,5,6]. Can you write that function? More on reddit.com
What does merge sort do?
The "merge" operation of merge sort takes two halves of an array, each of which has previously been sorted separately, and merges them together into a single sorted array. For example, given the two subarrays {3, 5, 11, 17} and {4, 8, 9, 20}, the merge operation will build a single array by choosing 3 from the first subarray, then 4 from the second, then 5 from the first, then 8 and 9 from the second, then 11 and 17 from the first, and then 20 from the second to produce the sorted array {3, 4, 5, 8, 9, 11, 17, 20}. So, if you have this merge operation and you want to sort an array, what do you do? Well, you sort the first half of the array, then you sort the second half of the array, and then you merge the two sorted halves together. Now, how do you sort each of the two halves of the array? For instance, how do you sort the first half? Well, you do it the same way: you sort the first subarray by splitting that into two halves, sorting each half, and then merging the two halves together. How do you sort the first half of that? The same way: split it into two halves, sort each half, and then merge them together. So you're splitting, splitting, splitting the array into smaller and smaller pieces. Eventually you get to a point where you've split the array into such small pieces that the thing you have to do is to sort an array of length 1. But that's easy—an array of length 1 is already sorted. Example: Use merge sort to sort the array {11, 28, 10, 19, 5, 21, 16, 14}. Sort the first half: {11, 28, 10, 19}. Sort the first half: {11, 28}. Sort the first half: {11}. This is an array of length 1, so it is already sorted. Sort the second half: {28}. This is an array of length 1, so it is already sorted. Merge the two sorted arrays together: merge {11} and {28} to get {11, 28}. Sort the second half: {10, 19}. Sort the first half: {10}. This is an array of length 1, so it is already sorted. Sort the second half: {19}. This is an array of length 1, so it is already sorted. Merge the two sorted arrays together: merge {10} and {19} to get {10, 19}. Merge the two sorted arrays together: merge {11, 28} and {10, 19} to get {10, 11, 19, 28}. Sort the second half: {5, 21, 16, 14}. Sort the first half: {5, 21}. Sort the first half: {5}. This is an array of length 1, so it is already sorted. Sort the second half: {21}. This is an array of length 1, so it is already sorted. Merge the two sorted arrays together: merge {5} and {21} to get {5, 21}. Sort the second half: {16, 14}. Sort the first half: {16}. This is an array of length 1, so it is already sorted. Sort the second half: {14}. This is an array of length 1, so it is already sorted. Merge the two sorted arrays together: merge {16} and {14} to get {14, 16}. Merge the two sorted arrays together: merge {5, 21} and {14, 16} to get {5, 14, 16, 21}. Merge the two sorted arrays together: merge {10, 11, 19, 28} and {5, 14, 16, 21} to get {5, 10, 11, 14, 16, 19, 21, 28}. More on reddit.com
Can someone explain to me the code behind a merge sort?
Any of the thousands of videos on YouTube about mergesort or the hundreds of textbooks that contain information about it will be superior to some lackluster explanation on here. If you're still confused after that, you should ask a specific question. More on reddit.com
Does your average programmer actually know the answer to those interview type questions on top of their head, like how to do a merge sort from scratch with no googling?
I certainly don't have the code for a merge sort memorized, but I understand the algorithm well enough to reconstruct it if somebody gives me a bit of time. Something like this: Let's see... I remember that it's a recursive divide-and-conquer algorithm, so I guess I'll split the list in half and sort each half. I want to make sure the two halves don't overlap, so the first half will go up to but not including the midpoint, and the second half will have everything from the midpoint onward. Checking through some edge cases like n=0, n=1 and n=2 on paper, I can see that my base case is when n<=1 in which case the list is automatically guaranteed to be sorted, so I can just return it as-is. Otherwise, choosing midpoint=n/2 (with integer division rounding down) guarantees that both halves are non-empty and smaller than the original list, so my recursive subdivision is guaranteed to make progress at each step. Cool. Now, after making recursive calls to sort the two halves, I can assume they're individually sorted, so all I have to do is merge them together and I'm good to go. How does that work again? Well, if I'm building the result in order, then first I need to add the smallest value, which is going to be either the minimum of the first half or the minimum of the second half, whichever is smaller. So I guess I'll compare left[0] and right[0], and then... oh yeah, now I remember that I basically just need to do the same thing in a loop, which means I need two index pointers to keep track of how many elements I've already copied from each list. At every loop iteration, I know I need to copy over whichever of the two "next" elements is smaller, because that's the one that needs to go first. Hmm, what happens if they're equal? I guess if both values are the same, I can just copy both and increment both pointers. Or, actually, I can just copy one of them arbitrarily, and then the other one will automatically get handled by the next iteration. That second option seems slightly simpler, so let's do that. Obviously the loop needs to end sometime... how do I do that? If one pointer hits the end of its array, then I'm in trouble because I don't have two elements to compare. But if that happens, I know the rest of the elements in the other array have to go at the end, because they're larger than the ones I already copied. So I guess I can just stop whenever either pointer exceeds its array bounds, and then check after the main loop to see if I need to copy any elements from the other one. Now I just need to translate that into code and step through it to make sure I didn't forget any edge cases or make any silly off-by-one errors. More on reddit.com
Videos
03:03
Merge sort in 3 minutes - YouTube
Understanding Mergesort: Sorting Made Simple | Recursion Series ...
13:45
Learn Merge Sort in 13 minutes 🔪 - YouTube
09:52
L-3.3: How Merge Sort Works?? Full explanation with example - YouTube
49:43
Merge Sort | Algorithm | Pseudocode | Dry Run | Code | Strivers ...
10:09
Learn MERGE SORT in 10 minutes! (Explanation and Code) - YouTube
W3Schools
w3schools.com › dsa › dsa_algo_mergesort.php
DSA Merge Sort
After the array is split, the sorting function calls itself with each half, so that the array can be split again recursively. The splitting stops when a sub-array only consists of one element. At the end of the Merge Sort function the sub-arrays are merged so that the sub-arrays are always sorted as the array is built back up.
Programiz
programiz.com › dsa › merge-sort
Merge Sort (With Code in Python/C++/Java/C)
As shown in the image below, the merge sort algorithm recursively divides the array into halves until we reach the base case of array with 1 element.
HackerEarth
hackerearth.com › practice › algorithms › sorting › merge sort
Merge Sort Tutorials & Notes | Algorithms | HackerEarth
Merge sort is a divide-and-conquer algorithm based on the idea of breaking down a list into several sub-lists until each sublist consists of a single element and merging those sublists in a manner that results into a sorted list.
TutorialsPoint
tutorialspoint.com › data_structures_algorithms › merge_sort_algorithm.htm
Merge Sort Algorithm
Merge sort is a sorting technique based on divide and conquer technique. With worst-case time complexity being (n log n), it is one of the most used and approached algorithms. Merge sort first divides the array into equal halves and then combines
Enjoy Algorithms
enjoyalgorithms.com › blog › merge-sort-algorithm
Merge Sort Algorithm
Merge sort is one of the fastest comparison-based sorting algorithms, which works on the idea of a divide and conquer approach. The worst and best-case time complexity of the merge sort is O(nlogn), and the space complexity is O(n). It is also one of the best algorithms for sorting linked lists ...
takeuforward
takeuforward.org › data-structure › merge-sort-algorithm
Merge Sort Algorithm - Tutorial
Search for a command to run
Khan Academy
khanacademy.org › computing › computer-science › algorithms › merge-sort › a › analysis-of-merge-sort
Analysis of merge sort (article)
We cannot provide a description for this page right now
DigitalOcean
digitalocean.com › community › tutorials › merge-sort-algorithm-java-c-python
Merge Sort Algorithm - Java, C, and Python Implementation | DigitalOcean
August 3, 2022 - Take adjacent pairs of two single-element array and merge them to form an array of 2 elements. Repeat the process till a single sorted array is obtained.
Great Learning
mygreatlearning.com › blog › it/software development › merge sort using c, c++, java, and python | what is merge sort and examples of it?
Merge Sort Using C, C++, Java, and Python | What is Merge Sort and Examples of it?
September 3, 2024 - MergeSort(arr, left, right): if left > right return mid = (left+right)/2 mergeSort(arr, left, mid) mergeSort(arr, mid+1, right) merge(arr, left, mid, right) end · In the worst case, in every iteration, we are dividing the problem into further 2 subproblems. Hence this will perform log n operations and this has to be done for n iteration resulting in n log n operations total. In the best case that is sorted array, we can do some modification by using a flag to check whether the lament is already sorted or not
Khan Academy
khanacademy.org › computing › computer-science › algorithms › merge-sort › a › overview-of-merge-sort
Merge sort algorithm overview (article)
We cannot provide a description for this page right now
Medium
medium.com › geekculture › understanding-merge-sort-78d93c433ad6
Understanding Merge Sort. When it comes to sorting algorithms… | by David Hammond | Geek Culture | Medium
June 4, 2021 - One and two are merged together, then we step up one level and we are ready for merge to be completed on [1,2] and the 7. At this point, we have completed the original calls to mergeSort on the left and mergeSort on the right, and finally, we are ready to complete the final call left on the execution stack which merges the final two arrays and leaves us with our sorted result of [1, 2, 3, 5, 7, 8, 9].
W3Schools
w3schools.com › python › python_dsa_mergesort.asp
DSA Merge Sort with Python
An array with values that needs to be sorted. A function that takes an array, splits it in two, and calls itself with each half of that array so that the arrays are split again and again recursively, until a sub-array only consist of one value. Another function that merges the sub-arrays back together in a sorted way.
Runestone Academy
runestone.academy › ns › books › published › pythonds › SortSearch › TheMergeSort.html
6.11. The Merge Sort — Problem Solving with Algorithms and Data Structures
If the list has more than one item, ... fundamental operation, called a merge, is performed. Merging is the process of taking two smaller sorted lists and combining them together into a single, sorted, new list....
Runestone Academy
runestone.academy › ns › books › published › pythonds3 › SortSearch › TheMergeSort.html
5.11. The Merge Sort — Problem Solving with Algorithms and Data Structures 3rd edition
We now turn our attention to using a divide and conquer strategy as a way to improve the performance of sorting algorithms. The first algorithm we will study is the merge sort. Merge sort is a recursive algorithm that continually splits a list in half. If the list is empty or has one item, ...
Princeton University
algs4.cs.princeton.edu › 22mergesort
Mergesort
March 20, 2021 - The algorithms that we consider ... itself to a simple recursive sort method known as mergesort: to sort an array, divide it into two halves, sort the two halves (recursively), and then merge the results....
