VOOZH about

URL: https://www.geeksforgeeks.org/dsa/merge-sort/

⇱ Merge Sort - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Merge Sort

Last Updated : 3 Oct, 2025

Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the Divide and Conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array.

👁 arr_

Here's a step-by-step explanation of how merge sort works:

  1. Divide: Divide the list or array recursively into two halves until it can no more be divided.
  2. Conquer: Each subarray is sorted individually using the merge sort algorithm.
  3. Merge: The sorted subarrays are merged back together in sorted order. The process continues until all elements from both subarrays have been merged.

Let's sort the array or list [38, 27, 43, 10] using Merge Sort

Let's look at the working of above example:

Divide:

  • [38, 27, 43, 10] is divided into [38, 27] and [43, 10] .
  • [38, 27] is divided into [38] and [27] .
  • [43, 10] is divided into [43] and [10] .

Conquer:

  • [38] is already sorted.
  • [27] is already sorted.
  • [43] is already sorted.
  • [10] is already sorted.

Merge:

  • Merge [38] and [27] to get [27, 38] .
  • Merge [43] and [10] to get [10,43] .
  • Merge [27, 38] and [10,43] to get the final sorted list [10, 27, 38, 43]

Therefore, the sorted list is [10, 27, 38, 43] .


Output
10 27 38 43 

Recurrence Relation of Merge Sort

The recurrence relation of merge sort is:

  • T(n) Represents the total time time taken by the algorithm to sort an array of size n.
  • 2T(n/2) represents time taken by the algorithm to recursively sort the two halves of the array. Since each half has n/2 elements, we have two recursive calls with input size as (n/2).
  • O(n) represents the time taken to merge the two sorted halves

Complexity Analysis of Merge Sort

Time Complexity:

  • Best Case: O(n log n), When the array is already sorted or nearly sorted.
  • Average Case: O(n log n), When the array is randomly ordered.
  • Worst Case: O(n log n), When the array is sorted in reverse order.

Auxiliary Space: O(n), Additional space is required for the temporary array used during merging.

Applications of Merge Sort:

Advantages and Disadvantages of Merge Sort

Advantages

  • 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 it performs well even on large datasets.
  • Simple to implement: The divide-and-conquer approach is straightforward.
  • Naturally Parallel : We independently merge subarrays that makes it suitable for parallel processing.

Disadvantages

  • Space complexity: Merge sort requires additional memory to store the merged sub-arrays during the sorting process.
  • Not in-place: Merge sort is not an in-place sorting algorithm, which means it requires additional memory to store the sorted data. This can be a disadvantage in applications where memory usage is a concern.
  • Merge Sort is Slower than QuickSort in general as QuickSort is more cache friendly because it works in-place.

Quick Links:

Comment