复习中,没有什么可说的~~~
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 100
int array[N];
void merge(int a[],int s,int mid,int e);
void msort(int a[],int s,int e)
{
if(s==e) return;
else
{
int mid=(s+e)/2;
msort(a,s,mid);
msort(a,mid+1,e);
merge(a,s,mid,e);
}
}
void merge(int a[],int s,int mid,int e)
{
int i,j,k;
int temp[N];
for(i=s,j=mid+1,k=0;i<=mid&&j<=e;)
if(a[i]<a[j]) temp[k++]=a[i++];
else temp[k++]=a[j++];
while(i<=mid) temp[k++]=a[i++];
while(j<=e) temp[k++]=a[j++];
for(i=s,j=0;i<=e;i++) a[i]=temp[j++];
}
int main()
{
int i;
for(i=0;i<20;i++)
array[i]=rand()%100;
msort(array,0,20);
for(i=0;i<20;i++)
printf("%d ",array[i]);
printf("\n");
return 0;
}
0 0 5 24 27 27 34 36 41 42 45 58 61 62 64 67 69 78 81 91
Press any key to continue