#include<iostream>
using namespace std;
int SR[10];
int TR[10];
void Merge(int i,int m,int n)
{
int k=0;
int low=i;
int j=m+1;
while(i<=m&&j<=n)
{
if(SR[i]<SR[j])
{
TR[k++]=SR[i++];
}
else
{
TR[k++]=SR[j++];
}
}
if(i<=m)
{
for(i;i<=m;i++)
{
TR[k++]=SR[i];
}
}
if(j<=n)
{
for(j;j<=n;j++)
{
TR[k++]=SR[j];
}
}
for(i=low,k=0;i<=n;i++,k++)
{
SR[i]=TR[k];
}
}
void MergeSort(int n)
{
int k=1;
int low,mid,high;
int size=1;
while(size<n)
{
low=0;
while(low+size<n)
{
mid=low+size-1;
high=mid+size;
if(high>n-1)
{
high=n-1;
}
Merge(low,mid,high);
cout<<"low:"<<low<<" mid: "<<mid<<" high :"<<high<<endl;
low=high+1;
}
size*=2;
}
}
int main()
{
for(int i=0;i<10;i++)
{
cin>>SR[i];
}
MergeSort(10);
for(int i=0;i<10;i++)
{
cout<<SR[i]<<" ";
}
return 0;
}
归并排序非递归
最新推荐文章于 2024-01-16 23:07:55 发布