Merge Sort Algorithm


Merge sort works as follows:

  1. Divide the unsorted list into n sublists, each containing one element. A list of one element is sorted by definition.
  2. Repeatedly merge sorted sublists (called "runs") to produce longer runs until there is only one run remaining. This is the sorted list.

    Pseudocode

    This algorithm makes use of a variable length temporary array, which is most easily represented in C++ using the vector class from the standard library. When implementing the algorithm, include the following code at the top of the source file:

    #include <vector>
    
    using std::vector;
    

    procedure merge_sort(array : list of sortable items, start : first element of list, 
    end : last element of list)
        if start < end
            mid ← (start + end) / 2
        
            merge_sort(array, start, mid)
            merge_sort(array, mid + 1, end)
        
            merge(array, start, mid, end)
        end if
    end procedure

    procedure merge(array : list of items to merge, start : first element of first sublist, mid : last element of first sublist,
      end : last element of second sublist)
        
        // Temporary vector of size (end - start + 1)
        vector<int> temp(end - start + 1);
        
        i ← start
        j ← mid + 1
        k ← 0
        
        while i <= mid and j <= end
            if array[i] < array[j]
                temp[k] ← array[i]
                i ← i + 1
            else
                temp[k] ← array[j]
                j ← j + 1
            end if
            k ← k + 1
        end while
        
        while i <= mid
            temp[k] ← array[i]
            i ← i + 1
            k ← k + 1
        end while
        
        while j <= end
            temp[k] ← array[j]
            j ← j + 1
            k ← k + 1
        end while
        
        Copy the elements of the vector temp back into array
        
    end procedure

    Example

    In this example, the merge sort merge() function is called with the arguments array (the array to partition), 0 (the subscript of the first element of the array), and 11 (the subscript of the last element of the array).

    Begin merge

    i ← start
    j ← mid + 1
    k ← 0
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp                        
     

    i <= mid and j <= end; loop entered. array[i] < array[j]

    temp[k] ← array[i]
    i ← i + 1
    k ← k + 1
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp 13                      
     

    i <= mid and j <= end; loop continues. array[i] not < array[j]

    temp[k] ← array[j]
    j ← j + 1
    k ← k + 1
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp 13 14                    
     

    i <= mid and j <= end; loop continues. array[i] < array[j]

    temp[k] ← array[i]
    i ← i + 1
    k ← k + 1
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp 13 14 22                  
     

    i <= mid and j <= end; loop continues. array[i] < array[j]

    temp[k] ← array[i]
    i ← i + 1
    k ← k + 1
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp 13 14 22 31                
     

    i <= mid and j <= end; loop continues. array[i] not < array[j]

    temp[k] ← array[j]
    j ← j + 1
    k ← k + 1
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp 13 14 22 31 45              
     

    i <= mid and j <= end; loop continues. array[i] < array[j]

    temp[k] ← array[i]
    i ← i + 1
    k ← k + 1
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp 13 14 22 31 45 48            
     

    i <= mid and j <= end; loop continues. array[i] not < array[j]

    temp[k] ← array[j]
    j ← j + 1
    k ← k + 1
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp 13 14 22 31 45 48 58          
     

    i <= mid and j <= end; loop continues. array[i] < array[j]

    temp[k] ← array[i]
    i ← i + 1
    k ← k + 1
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp 13 14 22 31 45 48 58 67        
     

    i <= mid and j <= end; loop continues. array[i] < not array[j]

    temp[k] ← array[j]
    j ← j + 1
    k ← k + 1
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp 13 14 22 31 45 48 58 67 72      
     

    i <= mid and j <= end; loop continues. array[i] < not array[j]

    temp[k] ← array[j]
    j ← j + 1
    k ← k + 1
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp 13 14 22 31 45 48 58 67 72 79    
     

    i <= mid and j <= end; loop continues. array[i] < array[j]

    temp[k] ← array[i]
    i ← i + 1
    k ← k + 1
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp 13 14 22 31 45 48 58 67 72 79 81  
     

    i not <= mid; loop ends. i not <= mid; loop not entered. j <= end; loop entered.

    temp[k] ← array[j]
    j ← j + 1
    k ← k + 1
    

      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    array 13 22 31 48 67 81 14 45 58 72 79 93
     
      [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
    temp 13 14 22 31 45 48 58 67 72 79 81 93
     

    Complexity

    Time Complexity: O(n log n)

    Space Complexity: O(n)