归并排序实现
#include<iostream> #include<stack> using namespace std; void merge(int *a ,int low, int mid ,int high) { stack<int> s; int i=low,j=mid+1; while(i<=mid&&j<=high) { if(a[i]<a[j]) s.push(a[i++]); else s.push(a[j++]); } while(i<=mid) s.push(a[i++]); while(j<=high) s.push(a[j++]); for(i=high;i>=low;i--) { a[i]=s.top(); s.pop(); } } void mergesort(int *a ,int low , int high) { if(low>=high) return; int mid = (low+high)/2; mergesort(a,low,mid); mergesort(a,mid+1,high); merge(a,low,mid,high); } void main() { int a[100]; int i; for(i=0;i<100;i++) a[i]=rand()%10000; mergesort(a,0,99); for(i=0;i<100;i++) cout<<a[i]<<endl; }
posted on 2012-12-10 22:51 luckistmaomao 阅读(107) 评论(0) 编辑 收藏 举报