Merge Sort/merge_sort.cpp
// merge the two halves into a sorted array
merge the two halves into a sorted array
merge the two halves into a sorted array
void Merge(int *a, int from, int to, int mid) {
    int i = from;
    int j = mid + 1;
    int k = 0;
    int temp[to-from+1];
    while (i <= mid && j <= to) {
        if (a[i] < a[j]) {
            temp[k] = a[i];
            k++; i++;
        }
        else {
            temp[k] = a[j];
            k++; j++;
        }
    }
    while (i <= mid) {
        temp[k] = a[i];
        k++; i++;
    }
    while (j <= to) {
        temp[k] = a[j];
        k++; j++;
    }
    for (i = from; i <= to; i++) {
        a[i] = temp[i-from];
    }
}
void Merge ( int * a , int from , int to , int mid ) { int i = from ; int j = mid + 1 ; int k = 0 ; int temp [ to - from + 1 ] ; while ( i <= mid && j <= to ) { if ( a [ i ] < a [ j ] ) { temp [ k ] = a [ i ] ; k ++ ; i ++ ; } else { temp [ k ] = a [ j ] ; k ++ ; j ++ ; } } while ( i <= mid ) { temp [ k ] = a [ i ] ; k ++ ; i ++ ; } while ( j <= to ) { temp [ k ] = a [ j ] ; k ++ ; j ++ ; } for ( i = from ; i <= to ; i ++ ) { a [ i ] = temp [ i - from ] ; } }
merge the two halves into a sorted array
merge the two halves into a sorted array
// merge the two parts into temp[] // insert all the remaining values from i to mid into temp[] // insert all the remaining values from j to to into temp[] // assign sorted data stored in temp[] to a[]