写了个二路归并的归并排序小代码,直接贴上来

/*
 file:quick.cpp
 author:www.5dkx.com
*/
#include<iostream>
using namespace std;
void Merge(int a[],int low,int mid,int high,int b[]);
void MSort(int a[],int low,int high,int b[]);
void main()
{
 int a[]={4,5,9,10,51,6,46,36,6,56,67,45,36};
 int b[13];
 MSort(a,0,12,b);
 for(int i=0;i<13;i++)
  cout<<b[i]<<" ";
 cout<<endl;
 for(int j=0;j<13;j++)
  cout<<a[j]<<" ";
 cout<<endl;
}

void Merge(int a[],int low,int mid,int high,int b[])
{
 int i=low,j=mid+1,k=low;
 while((i<=mid)&&(j<=high))
 {
  if(a[i]<=a[j])
  {
   b[k]=a[i];
   i++;
  }
  else
  {
   b[k]=a[j];
   j++;
  }
  k++;
 }
 while(i<=mid)
 {
  a[k]=a[i];
  k++;
  i++;
 }

 while(j<=high)
 {
  a[k]=a[j];
  k++;j++;
 }

}

void MSort(int a[],int low,int high,int b[])
{
 if(low==high)
  b[low]=a[low];
 else
 {
  int mid=(low+high)/2;
  MSort(a,low,mid,b);
  MSort(a,mid+1,high,b);
  Merge(a,low,mid,high,b);
 }
}

 

首发:http://www.5dkx.com/arch/67.html

非特别说明均为原创文章如转载,请注明:转载自 5D开心博客 [ http://www.5DKX.com/ ]

posted on 2010-03-31 21:37  5D开心博客  阅读(751)  评论(0编辑  收藏  举报