badge icon

This article was automatically translated from the original Turkish version.

Article

Divide and Conquer Algorithm: Merge Sort

Quote

Divide and Conquer algorithms are an approach that solves a large problem by breaking it down into smaller smaller subproblems. These algorithms typically consist of three stages:


  1. Divide: The problem is divided into smaller and more manageable subproblems.
  2. Conquer: The subproblems are solved independently.
  3. Combine: The solutions to the subproblems are combined to obtain the solution to the main problem.


This method is effectively used in many computer science problems such as sorting, searching, matrix multiplication, and optimal route calculations like.

Finding the Maximum Element Using the Divide and Conquer Algorithm

The most basic method for finding the largest element in an array is a double-loop comparison with O(n²) complexity. However, using the Divide and Conquer algorithm, this operation can be performed with O(n log n) or even O(log n) time complexity.

Classic Method: O(n²) Complexity

Finding the maximum element using two nested loops requires comparing each element with all others:

This method is inefficient because each element is unnecessarily compared multiple times.

Finding the Maximum Using the Divide and Conquer Algorithm: O(n) Complexity

The core idea of finding the maximum element using the Divide and Conquer algorithm is to recursively divide the array into halves to determine the largest element. This method is expressed by the recurrence relation T(n) = 2T(n/2) + O(1) and has O(n) time complexity.


A similar algorithm can be used to find the minimum element. In this case, the minimum element from each subarray is returned instead of the maximum to reach the final result.

Merge Sort Algorithm

The Merge Sort Algorithm is a stable and efficient sorting algorithm that uses the Divide and Conquer strategy. The fundamental working principle of the algorithm is as follows:


  1. The array is divided into two equal parts.
  2. Each part is sorted separately.
  3. The two sorted parts are merged together in a merge step to produce a fully sorted array.


This process continues until the array is broken down into single-element subarrays, after which the subarrays are sorted and merged.

Working Principle of the Merge Sort Algorithm

The Merge Sort Algorithm follows these steps:

  1. Divide Phase:
    1. The array is split into two halves at the midpoint.
    2. This process continues until the array is completely divided.
  2. Conquer Phase:
    1. Single-element arrays are naturally sorted.
    2. Once recursive calls are completed, the sorting process begins.
  3. Merge Phase:
    1. Two sorted subarrays are merged by comparing their elements.
    2. Smaller elements are selected first to build a sorted structure.


Example of Merge Sort Algorithm steps


Merge Sort Algorithm in C++

Time and Space Complexity of the Merge Sort Algorithm

One of the most important features of the Merge Sort Algorithm is that it has O(n log n) time complexity in all cases.


  • Best Case: O(n log n)
  • Average Case: O(n log n)
  • Worst Case: O(n log n)


The space complexity is O(n), as it requires additional memory during the merge phase. Therefore, Merge Sort is generally suitable for large data sets where in-place sorting is not required.

Why Is the Time Complexity of Merge Sort O(N log N)?

Merge Sort is a sorting algorithm that operates on the Divide and Conquer principle. To understand its time complexity, two key stages must be examined.


  1. Divide Phase
  2. Merge Phase


1. Divide Phase (Divide) - O(log N)

The Merge Sort Algorithm divides the array into two halves at each step.

  • In the first phase, the array has N elements.
  • In the next phase, it is split into two arrays of size N/2.
  • Then each is split into four arrays of size N/4.

This process continues until only single elements remain. When an array is repeatedly halved, this process reaches a depth of log N.


For example:

If the array has 8 elements, the division steps are as follows:

  1. 8 elements → 4 elements → 2 elements → 1 element (4 levels)
  2. In general: log₂(N) levels

Therefore, the divide phase takes O(log N) time.


2. Merge Phase (Merge) - O(N)

After the division is complete, each subarray is sorted and merged.

  • In the first phase, individual sorted elements are merged → done in O(N) time.
  • In the second phase, larger arrays are merged, again totaling O(N) operations.


A total of O(N) operations are performed at every level.


Advantages and Disadvantages of Merge Sort

Advantages

  • Stable Sorting: Elements with equal values retain their relative positions after sorting.
  • Suitable for Large Data Sets: Its performance is consistently O(n log n), providing stable performance with large data sets.
  • Can Be Divided into Independent Subproblems: Suitable for parallel computation.

Disadvantages

  • Additional Memory Usage: Requires auxiliary arrays, resulting in an O(n) additional memory requirement.
  • Inefficient for Small Data Sets: Due to its need for extra memory, it can be slower than algorithms like Insertion Sort for small arrays.


Applications of Merge Sort

Merge Sort is widely used in applications requiring large data sets and stable sorting:

  • Database sorting operations
  • File sorting and large-scale data processing
  • Parallel computing and multi-threaded systems
  • Sorting operations on linked lists

Bibliographies

C++ Reference. (n.d.). *std::vector*. Accessedhttps://cplusplus.com/reference/vector(Vektör kütüphanesi fonksiyonları ve kullanımı)

Cormen, T. H., Leiserson, C. E., Rivest, R. L., and Stein, C. (2009). Introduction to Algorithms (3rd ed.). The MIT Press. pp. 30-39

Dasgupta, S., Papadimitriou, C. H., and Vazirani, U. (2006). Algorithms. McGraw-Hill. pp. 54-57

GeeksforGeeks. (n.d.). Introduction to Divide and Conquer Algorithm.Accessed

GeeksforGeeks. (n.d.). Merge Sort Algorithm. Accessed.https://www.geeksforgeeks.org/merge-sort/

Sedgewick, R., and Wayne, K. (2011). Algorithms (4th ed.). Addison-Wesley. pp. 270-280

Stanford University Lecture Notes. (n.d.). *Sorting and Divide & Conquer Algorithms.* pp. 20–70.

Author Information

Avatar
AuthorAbdulsamet EkinciJuly 14, 2026 at 2:48 PM

Discussions

No Discussion Added Yet

Start discussion for "Divide and Conquer Algorithm: Merge Sort" article

View Discussions

Contents

  • Finding the Maximum Element Using the Divide and Conquer Algorithm

    • Classic Method: O(n²) Complexity

    • Finding the Maximum Using the Divide and Conquer Algorithm: O(n) Complexity

  • Merge Sort Algorithm

    • Working Principle of the Merge Sort Algorithm

    • Merge Sort Algorithm in C++

    • Time and Space Complexity of the Merge Sort Algorithm

    • Why Is the Time Complexity of Merge Sort O(N log N)?

      • 1. Divide Phase (Divide) - O(log N)

      • 2. Merge Phase (Merge) - O(N)

    • Advantages and Disadvantages of Merge Sort

      • Advantages

      • Disadvantages

    • Applications of Merge Sort