#include<iostream>
using namespace std;
void DoMerge(int array[], int buff[], int begin, int middle, int end){
int leftHalfBegin = begin;
int leftHalfEnd = middle;
int rightHalfBegin = middle+1;
int rightHalfEnd = end;
int index = begin;
while (leftHalfBegin <= middle && rightHalfBegin <= end){
if (array[leftHalfBegin] < array[rightHalfBegin]){
buff[index] = array[leftHalfBegin];
index++;
leftHalfBegin++;
}else{
buff[index] = array[rightHalfBegin];
index++;
rightHalfBegin++;
}
}
while (leftHalfBegin <= middle){
buff[index++] = array[leftHalfBegin++];
}
while(rightHalfBegin <= end)
{
buff[index++] = array[rightHalfBegin++];
}
for(int i = begin; i<= end;i++){
array[i] = buff[i];
}
}
void MergeSort(int array[], int buff[], int begin, int end)
{
int middle = (begin+end)/2;
if (begin < end){
MergeSort(array, buff, begin, middle);
MergeSort(array, buff, middle + 1, end);
DoMerge(array, buff, begin, middle, end);
}
}
int main(){
int array[6]={3,5,9,8,7,5};
int buff[6];
MergeSort(array, buff, 0, 5);
for(int i =0;i<6;i++){cout<<array[i]<<endl;}
}