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
worst-case optimal stable divide and conquer comparison sorting algorithm
Wikipedia
en.wikipedia.org › wiki › Merge_sort
Merge sort - Wikipedia
1 week 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.
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
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
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
49:43
Merge Sort | Algorithm | Pseudocode | Dry Run | Code | Strivers ...
10:47
Merge Sort: A Simple Step-by-Step Walkthrough 😀 - YouTube
Merge Sort - 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.
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 ...
Reddit
reddit.com › r/learnprogramming › can someone explain to me the code behind a merge sort?
r/learnprogramming on Reddit: Can someone explain to me the code behind a merge sort?
October 15, 2018 -
My professor was going over this a bit but I'm really not sure I understand what she's talking about. As I understand it uses recursion and continously divides an array until you have a bunch of smaller arrays which you can sort like a binary choice, and then those get sorted together backwards like 2|1 gets sorted into 1|2 then combined with another array like 1|2|3|4 and then so on so forth until you have a full sized sorted array on the other side? Is that right? How does the code work?
Top answer 1 of 4
2
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.
2 of 4
2
Is that right? That's a great description. How does the code work? Here's another way to think about it. I'm going to provide you with a magical magic_sort function. You don't know how it works, just that you provide it with an array of data and it will sort it. The problem is that my magic_sort function doesn't work on large arrays. If you wanted to sort a list of 16 things, my function can only sort 8 at a time. Your challenge is to write a sort function that is capable of sorting 16 things, using my function to help out. How would you do that? Well, one way (not the only way, mind you) is to use my function to sort two halves of the data separately. So if you have the numbers: 13 10 12 8 6 2 3 16 1 11 14 7 4 9 5 15 You break it into two sets of 8: 13 10 12 8 6 2 3 16 1 11 14 7 4 9 5 15 And have my magic_sort function do its magic to get: 2 3 6 8 10 12 13 16 1 4 5 7 9 11 14 15 Of course, we're not quite done yet. Your function still has some work to do: merging those two lists together. This is slightly tricky to implement, but essentially you pick the smallest of the two lists (1 and 2) and put that in the final result and then move to the next larger in that list (4). You then pick the smallest of those two (2 and 4) and move to the next larger in that list (3). Repeat until the lists are merged together. You should see at this point that the final list is perfectly sorted. Mission accomplished! The code should look something like this (in C-like pseudocode) void sort(list) { magic_sort(list[left_half]); magic_sort(list[right_half]); list = merge(left_half, right_half); } Now for the fun part: what about my magic_sort function? I ... uh ... lied to you. I forgot to write it. But that's OK! The secret is that you never needed it, because you just wrote it! Instead of using magic_sort, why not just use sort itself? void sort(list) { sort(list[left_half]); sort(list[right_half]); list = merge(left_half, right_half); } But there's a problem. What happens if you have a list of just one item? It isn't even possible to split this list into two halves, but our algorithm doesn't know that yet, and will happily try to split it anyway. But really, a list with one item (or none) is already sorted, so there's nothing left to do: void sort(list) { if ( list.length <= 1 ) return; sort(list[left_half]); sort(list[right_half]); list = merge(left_half, right_half); } ... And we're done! FYI ... this isn't the only way to solve the problem. An alternative is to first split the array in half: those smaller than average and those larger than average. Then you can do a magic_sort on the smaller half and a magic_sort on the larger half. No merge step is needed this time, since the halves are already guaranteed to be sorted relative to each other. This is what we call "quicksort." Partitioning the list is the hard step in this case (instead of merging), and you don't always use the average. But you'll study quicksort later, I just wanted to show how closely it's related to mergesort. Also, the general process I described works very well for coming up with recursive solutions to many problems, not just sorting. Pretend you have a function that does what you want already, but with a smaller set of data. Be creative to use that function to write your own. Substitute your new function for the pretend one. Locate the "base case" -- when the problem becomes atomic (can't be divided further) and produce the needed result in that case instead of using recursion. That's all there is to it.
takeuforward
takeuforward.org › data-structure › merge-sort-algorithm
Merge Sort Algorithm - Tutorial
Search for a command to run
Medium
medium.com › sorting-algorithms › introduction-to-merge-sort-algorithm-why-is-it-so-efficient-ef96f7feef70
Introduction to Merge Sort Algorithm: How It Works and Why It’s So Efficient | by Andrea Cappelletti | Sorting Algorithms | Medium
January 25, 2021 - We can now compare them in exactly the same manner that they were broken down: we compare the element for each list and then we combine them a new array in a sorted manner. Let’s consider an array A and divide it into subarrays (A1, A2,…An) until we obtain the An array with only one element. ... Then we split it into two subarrays: left subarray and right subarray. ... Now it’s time to merge!
Instagram
instagram.com › popular › how-do-you-implement-a-merge-sort-algorithm
How Do You Implement A Merge Sort Algorithm
We cannot provide a description for this page right now
Nhrec
nhrec.org › ~schinnab › Merge_Sort.html
Analysis
b) The “mergeSort” method: Merges the two sorted sub-arrays into a single, sorted array.
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
IIT Kanpur
cse.iitk.ac.in › users › dsrkg › cs210 › applets › sorting › mergeSort › mergeSort.html
Merge Sort - CS245 Project By Joseph George
Merge Sort algorithm uses the process of merging two sorted lists recursively to accomplish the sorting of an unsorted list. Firstly, two sorted lists of length M and N respectively can be merged in O(M+N) time.
Instagram
instagram.com › popular › what-is-a-merge-sort
What Is A Merge Sort
We cannot provide a description for this page right now
30DaysCoding
blogs.30dayscoding.com › blogs › dsa › basic-algorithms › sorting-algorithms › merge-sort
Merge Sort
Once the array has been divided into individual elements or pairs, the merge operation begins. In this step, the algorithm combines the pairs of subarrays, comparing and merging their elements in sorted order.
Oxford University
mathcenter.oxford.emory.edu › site › cs171 › mergeSort
Merge Sort
Being "smaller" lists, the sorting of the left and right halves are easily done recursively, until they become lists of only one element -- which are trivially already sorted. The below diagram shows the recursive breakdown of a list of integers (up to the center row) and merging together of various pairs of smaller lists created into the final sorted array.
