#include<iostream> using namespace std; void MergeArray( int* a, int lhs, int mid, int rhs , int* temp) { int lhs_first = lhs, rhs_first = mid; int lhs_second = mid+1, rhs_second = rhs; int lhs_temp = lhs, rhs_temp = rhs; while( lhs_first <= rhs_first && lhs_second <= rhs_second ) { if( a[lhs_first] <= a[lhs_second] ) { temp[lhs_temp++] = a[lhs_first++]; } else { temp[lhs_temp++] = a[lhs_second++]; } } while( lhs_first <= rhs_first ) temp[lhs_temp++] = a[lhs_first++]; while(lhs_second <= rhs_second ) temp[lhs_temp++] = a[lhs_second++]; for( lhs_temp = lhs; lhs_temp <= rhs; lhs_temp++ ) a[lhs_temp] = temp[lhs_temp]; } void MergeSort( int* a, int lhs, int rhs, int* temp) { int mid; if( lhs < rhs ) { mid = ( lhs + rhs )/2; MergeSort( a, lhs, mid, temp ); MergeSort( a, mid+1, rhs, temp ); MergeArray( a, lhs, mid, rhs, temp ); } } void OutPut( int* a, int n) { for( int i = 0; i < n; i++ ) { cout<< a[i]<<" "; } cout<<endl; } int main() { int array[7] = {11, 7, 8, 6, 3, 4, 2}; int temp[7]={0}; OutPut( array, 7); MergeSort(array, 0, 6, temp); OutPut( array, 7); return 0; }