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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include<iostream>
using namespace std;
int v[100],b[100],n,i;
void showAr(int end, int start, int v[100])
{
for(i=end;i<=start;i++)
cout<<v[i];
cout<<endl;
}
void sortm(int end, int start,int v[100])
{
int aux;
if(v[start]>v[end])
{
aux=v[start];
v[start]=v[end];
v[end]=aux;
}
}
void mergeAr(int start, int end, int m, int v[100])
{
int b[100],k=1,i=start,j=m+1;
while(i<m&&j<end)
if(v[i]>v[j])
b[k++]=v[j++];
else
b[k++]=v[i++];
while(i<m)
b[k++]=v[i++];
while(j<end)
b[k++]=v[j++];
for(i=1;i<=end;i++)
v[i]=b[i];
}
void merge_sort(int start, int end, int v[100])
{
int m;
if((end-start)<=1)
sortm(end, start,v);
else
{
m=(start+end)/2;
merge_sort(start,m,v);
merge_sort(m+1,end,v);
mergeAr(start,end,m,v);
}
}
int main()
{
cout<<"n= "; cin>>n;
for(i=1;i<=n;i++)
cin>>v[i];
// for(i=1;i<=n;i++)
// cout<<v[i]<<" ";
merge_sort(1,n,v);
for(i=1;i<=n;i++)
cout<<v[i]<<" ";
}
|