๐ŸŒ
Programiz PRO
programiz.pro โ€บ resources โ€บ dsa-merge-sort-complexity
Exploring time and space complexity of Merge sort
Merge Sort is a comparison-based divide-and-conquer sorting algorithm that works by recursively dividing the array into halves, sorting each half, and then merging them back together. It consistently performs with a time complexity of O(n log n) in the best, worst, and average cases.
worst-case optimal stable divide and conquer comparison sorting algorithm
Merge-sort-example-300px.gif
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 โ€ฆ Wikipedia
Factsheet
Data structure Array
Worst-case performance
Factsheet
Data structure Array
Worst-case performance
Discussions

algorithms - Why is mergesort O(log n)? - Software Engineering Stack Exchange
IMO it makes more sense to count ... At each "merge stage", a total of (cn) work is being performed (as Shantanu has explained). So the total cost across all stages is: (cn)*(log(n)). ... Merge Sort is a recursive algorithm and time complexity can be expressed as following recurrence ... More on softwareengineering.stackexchange.com
๐ŸŒ softwareengineering.stackexchange.com
algorithm - What is the time complexity of the merge step of mergesort? - Stack Overflow
I know this algorithm has a time complexity of o(nlogn), but if we speak about only the merge step, is this one still o(nlogn)? Or is it reduced to o(logn)? I believe the second is the answer but s... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Trying to understand the time complexity of merge sort issue
We merge two lists of length N/2, which takes time N. In order to do that, we had to merge 4 lists of length N/4, which takes time N. In order to do that, we had to merge 8 lists of length N/8, which takes time N. At the very beginning we merged N lists of length 1. So each time we're doubling the number of lists, until we get to N. The number of times you have to double 1 until you get to N is log_2 (N). So that's where the log_2 comes from. Maybe that helps? More on reddit.com
๐ŸŒ r/algorithms
6
3
August 25, 2020
My Merge Sort gives me a stack overflow
You're missing parentheses. Try `let m = (l + r) / 2`. More on reddit.com
๐ŸŒ r/rust
12
4
October 25, 2020
People also ask

How does the merge sort algorithm work?
Merge sort is a divide-and-conquer algorithm that splits an array into halves, recursively sorts each half, and merges the sorted halves back together. It repeatedly divides arrays until subarrays of size one are achieved, then combines them in sorted order, resulting in a fully sorted array.
๐ŸŒ
vaia.com
vaia.com โ€บ merge sort
Merge Sort: Algorithm & Time Complexity | Vaia
Is merge sort a stable sorting algorithm?
Yes, merge sort is a stable sorting algorithm because it preserves the relative order of equal elements in the input array. This is achieved by ensuring that when merging two halves of the array, elements from the left half are placed before equal elements from the right half.
๐ŸŒ
vaia.com
vaia.com โ€บ merge sort
Merge Sort: Algorithm & Time Complexity | Vaia
What are the practical applications of merge sort?
Merge Sort is useful in scenarios where stability is crucial and when sorting linked lists due to its non-reliance on random access to data. It's often employed in external sorting algorithms, like sorting large datasets that don't fit in memory as it efficiently handles disk-based storage.
๐ŸŒ
vaia.com
vaia.com โ€บ merge sort
Merge Sort: Algorithm & Time Complexity | Vaia
๐ŸŒ
Vaia
vaia.com โ€บ merge sort
Merge Sort: Algorithm & Time Complexity | Vaia
August 4, 2023 - Let's delve deeper into the computational aspects of Merge Sort: The time complexity of Merge Sort is consistently O(n log n), making it ideal for data-intensive tasks where efficiency matters.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ merge-sort
Merge Sort - GeeksforGeeks
Stability : Merge sort is a stable sorting algorithm, which means it maintains the relative order of equal elements in the input array. Guaranteed worst-case performance: Merge sort has a worst-case time complexity of O(N logN) , which means ...
Published ย  October 3, 2025
๐ŸŒ
Alma Better
almabetter.com โ€บ bytes โ€บ articles โ€บ merge-sort-time-complexity
What is the Time Complexity of Merge Sort Algorithm?
June 12, 2024 - This process is repeated recursively until the entire array is sorted. Merge Sort has an average and worst-case time complexity of O(n log n), making it a reliable choice for sorting large datasets.
Top answer
1 of 5
74

It's O(n * log(n)), not O(log(n)). As you've accurately surmised, the entire input must be iterated through, and this must occur O(log(n)) times (the input can only be halved O(log(n)) times). n items iterated log(n) times gives O(n log(n)).

It's been proven that no comparison sort can operate faster than this. Only sorts that rely on a special property of the input such as radix sort can beat this complexity. The constant factors of mergesort are typically not that great though so algorithms with worse complexity can often take less time.

2 of 5
49

The complexity of merge sort is O(nlog(n)) and NOT O(log(n)).

Merge sort is a divide and conquer algorithm. Think of it in terms of 3 steps:

  1. The divide step computes the midpoint of each of the sub-arrays. Each of this step just takes O(1) time.
  2. The conquer step recursively sorts two subarrays of n/2 (for even n) elements each.
  3. The merge step merges n elements which takes O(n) time.

Now, for steps 1 and 3 i.e. between O(1) and O(n), O(n) is higher. Let's consider steps 1 and 3 take O(n) time in total. Say it is cn for some constant c.

How many times are we subdividing the original array?

We subdivide the input until each sub-array has one item so there are exactly log(n) "subdivision stages". We undo each subdivision stage with a "merge stage".

For example, if n = 8, there is a total of 3 merge stages: one where each pair of n/8 sub-arrays are merged to form a single n/4 sub-array, one where pairs of n/4s are merged to form n/2s, and one where the pair of n/2 are merged to form n.

What is the time cost for merging all pairs at each merge stage?

For this, look at the tree below - for each level from top to bottom:

  • Level 2 calls merge method on 2 sub-arrays of length n/2 each. The complexity here is 2 * (cn/2) = cn.
  • Level 3 calls merge method on 4 sub-arrays of length n/4 each. The complexity here is 4 * (cn/4) = cn.
  • and so on ...

At each merge stage, the total cost for merging all pairs is O(cn). Since there are log(n) merge stages, the total complexity is: (cost per stage)*(number of stages) = (cn)*(log(n)) or O(nlog(n)).

Image credits: Khan Academy

Find elsewhere
๐ŸŒ
Codecademy
codecademy.com โ€บ article โ€บ time-complexity-of-merge-sort
Time Complexity of Merge Sort: A Detailed Analysis | Codecademy
Explore the time complexity of Merge Sort in-depth, including best, average, and worst-case analysis, and comparison with other sorting algorithms.
๐ŸŒ
Medium
medium.com โ€บ karuna-sehgal โ€บ a-simplified-explanation-of-merge-sort-77089fe03bb2
A Simplified Explanation of Merge Sort | by Karuna Sehgal | Karuna Sehgal | Medium
February 5, 2018 - Merge Sort is a stable sort which means that the same element in an array maintain their original positions with respect to each other. Overall time complexity of Merge sort is O(nLogn).
๐ŸŒ
NVIDIA Developer
developer.nvidia.com โ€บ blog โ€บ merge-sort-explained-a-data-scientists-algorithm-guide
Merge Sort Explained: A Data Scientistโ€™s Algorithm Guide | NVIDIA Technical Blog
June 12, 2023 - The time complexity of the merge sort algorithm remains O(n log n) for best, worst, and average scenarios, making it suitable for sorting large lists and linked lists where stability is important.
๐ŸŒ
Youcademy
youcademy.org โ€บ merge-sort-time-space-complexity
Time and Space Complexity of Merge Sort
Merge Sort has a time complexity of O(n log n) in all cases: best, average, and worst. This makes it highly efficient compared to algorithms like Bubble Sort (O(nยฒ)) for large datasets.
๐ŸŒ
HappyCoders.eu
happycoders.eu โ€บ algorithms โ€บ merge-sort
Merge Sort โ€“ Algorithm, Source Code, Time Complexity
June 12, 2025 - We have now executed the merge ... of previously O(n). The total complexity of the sorting algorithm is, therefore, O(nยฒ log n) โ€“ instead of O(n log n)....
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ algorithms โ€บ sorting โ€บ when will the worst case of merge sort occur?
When Will the Worst Case of Merge Sort Occur? | Baeldung on Computer Science
March 18, 2024 - Step 2 of the algorithm includes โ€œMerge + Sortโ€, where two subarrays are merged so that a sorted array is created from each pair of subarrays. In the last step, the two halves of the original array are merged so that the complete array is sorted: This algorithm loops through times and the time complexity of every loop is , so the time complexity of the entire function is .
๐ŸŒ
Quora
quora.com โ€บ What-is-the-time-complexity-of-Merge-Sort-and-why-does-it-have-this-complexity
What is the time complexity of Merge Sort and why does it have this complexity? - Quora
Answer (1 of 2): The split step of Merge Sort will take O(n) instead of O(log(n)). If we have the runtime function of split step: T(n) = 2T(n/2) + O(1) with T(n) is the runtime for input size n, 2 is the number of new problems and n/2 is the ...
๐ŸŒ
Hero Vired
herovired.com โ€บ learning-hub โ€บ blogs โ€บ space-complexity-of-merge-sort
Time and Space Complexity of Merge Sort - Hero Vired
June 27, 2024 - Even in this worst case, the time complexity remains O(n log n). The key difference lies in the number of comparisons and swaps required to merge the sub-arrays. Merge Sort is known for its efficient time complexity, but it also requires additional ...
๐ŸŒ
W3Schools
w3schools.com โ€บ dsa โ€บ dsa_timecomplexity_mergesort.php
DSA Merge Sort Time Complexity
The number of splitting operations \((n-1)\) can be removed from the Big O calculation above because \( n \cdot \log_{2}n\) will dominate for large \( n\), and because of how we calculate time complexity for algorithms. The figure below shows how the time increases when running Merge Sort on an array with \(n\) values.
๐ŸŒ
guvi.in
studytonight.com โ€บ data-structures โ€บ merge-sort
merge sort time complexity
Time complexity of Merge Sort is O(n*Log n) in all the 3 cases (worst, average and best) as merge sort always divides the array in two halves and takes linear time to merge two halves.