Computer Algorithms II Lecture Notes

30 October 2007 • Divide and Conquer


template <typename T>
void
mergesort(
  T * const a_left, T * const a_right, 
  T * const temp_left, T * const temp_right) {

  const unsigned len = a_right - a_left;
  if (len > 1) {
    const unsigned h = len/2;
    T * const a_mid = a_left + h;
    T * const temp_mid = temp_left + h;

    mergesort(a_left, a_mid, temp_left, temp_mid);
    mergesort(a_mid, a_right, temp_mid, temp_right);
    merge(temp_left, temp_right, a_left, a_mid, a_right);
    }
  }


This page last modified on 24 January 2006.