#include<vector>
#include<iostream>
using namespace std;
void merge(vector<int> &va, int p, int q, int r)
{
int n1 = q - p + 1;
int n2 = r - q;
vector<int> L(n1+1);
vector<int> R(n2+1);
for( int i = 0; i < n1; i ++)
{
L[i] = va[p + i];
}
for( int j = 0; j < n2; j ++)
{
R[j] =va[q + 1 + j];
}
L[n1] = 65535;
R[n2] = 65535;
int a =0;
int b = 0;
for(int k = p; k < r + 1; k ++)
{
if( L[a] <= R[b])
{
va[k] = L[a];
a += 1;
}
else
{
va[k] = R[b];
b += 1;
}
}
}
void merge_sort( vector<int> &va, int p, int r)
{
if ( p < r )
{
int q = ( p + r ) / 2;
merge_sort( va, p, q );
merge_sort( va, q + 1, r );
merge( va, p, q, r );
}
}
int main()
{
int r = 7;
int p = 0;
int A[8] = { 2, 9, 1, 5, 8, 23, 3, 6 };
vector <int>va(A,A+8);
merge_sort(va, p, r);
for (int x = 0; x < 8; x ++)
{
cout << va[x] <<" "<< endl;
}
return 0;
}