Data Journey

International Studies Grad. racing for AI Engineer

Download as .zip Download as .tar.gz View on GitHub
15 January 2021

Merge Sort

by mervyn

Merge Sort

The merge sort algorithm is defined recursively:

Merging Example

Merge sort contains three things:

3, 5, 18, 21, 24, …
2, 7, 12, 16, 33, …
 
  1. Define three indices at the start of each array.
  2. Compare 2 and 3: 2 < 3
    • Copy 2 down
    • Increment the corresponding indices

Compare 3 and 7: 3 < 7

Compare 5 and 7: 5 < 7

Compare 18 and 7: 7 < 18

Compare 18 and 12: 12 < 18

Compare 18 and 16: 16 < 18

Compare 33 and 18: 18 < 33

Compare 21 and 33: 21 < 33

Compare 24 and 33: 24 < 33 - Copy 24 down - Increment the corresponding indices

  1. Continue until we have passed beyond the limit of one of the two arrays. == One of the sub-arrays gets ended (copied every element from 3 to 31 and there’s no more elements to compare in this sub-array)

  2. Copy over all remaining entries in the non-empty array (copy from 33 to 42 sequentially from the second sub-array to the result array)

Merging Two (Sorted) Arrays

Programming a merge is straight-forward:

Implementation

template <typename Type>
void Merge(Type *_arrayA, Type *_arrayB, int _nA, int _nB, Type *_arrayOut){
  int posA = 0, posB = 0, k = 0;
  while(posA < _nA && posB < _nB){
    if(_arrayA[posA]< _arrayB[posB]){
      _arrayOut[k] = _arrayA[posA];
      posA++;
    }
    else{
      _arrayOut[k] = _arrayB[posB];
      posB++;
    }
    k++;
  }
  for(; posA < _nA; posA++){
    _arrayOut[k] = _arrayA[posA];
    k++;
  }
  for(; posB< _nB; posB++){
    _arrayOut[k] = _arrayB[posB];
    k++;
  }
}

Problem of Merging

We cannot merge two arrays in-place

Algorithm

  1. Split the list into two approximately equal sub-lists
  2. Recursively call merge sort on both sub lists
  3. Merge the resulting sorted lists

Merge-Sort(A, p, r)

if p < r
   q = [(p + r)/2]
   Merge-Sort(A, p, q)
   Merge-Sort(A, q+1, r)
   Merge(A, p, q, r)

Implementation

Suppose that we already have a function

template <typename Type>
void Merge(Type *array, int first, int midpoint ,int last)

that assumes that the entries

array[first] through array[midpoint - 1], and
array[midpoint] through array[last - 1]

are sorted and merges these two sub-arrays into a single sorted array from index ‘first’ though index ‘last-1’. We will therefore implement a function

template <typename Type>
void Merge_Sort(Type *array, int first, int last)

that will sort the entries in the positions \(first \leq i < last\) If the number of entries is less than \(n_{T}\), call insertion sort Otherwise:

  1. Find the mid-point
  2. Call merge sort recursively on each of the halves, and
  3. Merge the results
template <typename Type>
void Merge_Sort(Type *array, int first, int last){
  if(_last - _first <= NUM_THRESHOLD){
    Insertion_Sort<Type>(_array, _first, _last);
  }// $$T(1)= T(2)= ... = T(N_{T})= \Theta(1)$$
  else{
    int midpoint=(_first + _last)/2;// $$\Theta(1)$$

    Merge_Sort<Type>(_array, _first, midpoint);//$$T\left ( \frac{n-1}{2} \right )$$
    Merge_Sort<Type>(_array, midpoint, _last);//$$T\left ( \frac{n-1}{2} \right )$$
    Merge(_array, _first, midpoint, _last);// $$\Theta(n)$$ linear time
  }  
}

Recursive Functions-Merge Sort

Recursion three Given a recurrence: \(T(n)\) = {if \(n = 1\), \(\Theta(1)\) {if \(n > 1\), \(2T(n/2)+\Theta(n)\) We first rewrite the recurrence as follows: \(T(n)\) = {if \(n = 1\), \(c\) {if \(n > 1\), \(2T(n/2) +cn\)

rectree

rectree2

Total Time Complexity: \(\Theta(n lg n)\)

source “K-MOOC 허재필 교수님의 <인공지능을 위한 알고리즘과 자료구조: 이론, 코딩, 그리고 컴퓨팅 사고> 강좌의 7-2 합병 정렬과 재귀적 알고리즘의 복잡도 분석 중(http://www.kmooc.kr/courses/course-v1:SKKUk+SKKU_46+2020_T1)”

back next

tags:

Comments

Post comment
Loading...

Share this: