1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
#include <vector>
using namespace std;
void Merg(vector<int> & A , int S ,int M, int E)
{
int size = E-S;
vector <int> temp; // only storing the branches;
for (int i = 0 ; i<int(A.size()) ;i++)
{
temp.push_back(A.at(i));
}
int J=S; //begining of the first branch
int K=M+1; // begining of the next branch
for(int i = S;i<=E-S;i++)
{
if(J<= M && temp.at(J)<temp.at(K))
{
A.at(i)=temp.at(J);
J++;
}
else
{
A.at(i)=temp.at(K);
if(K<(S-E))
{
K++;
}
}
}
}
//S == start , E=End ,middle = M, A == Array;
void Merg_Sort(vector<int> & A,int S,int E)
{
if (S < E)
{
int M =(S+E)/2;
Merg_Sort(A,S,M);
Merg_Sort(A,M+1,E);
Merg(A,S,M,E);
}
}
int main ()
{
vector <int> x;
for (int i =0;i<8;i++)
{
x.push_back(i);
}
x.at(2)=55;
Merg_Sort(x,0,7);
for (int i =0;i<int(x.size());i++)
{
cout<<x.at(i)<<endl;
}
}
|