归并排序算法
来源:互联网 发布:js中gripinnerhtml 编辑:程序博客网 时间:2024/06/15 10:19
自己想找找归并排序怎么实现的,结果在网上居然没找到一个能用的归并排序的算法,经过自己的琢磨,现在写出了下面的算法。
void MergeArray(int *a,int first,int middle,int last){int n1 = middle-first;int n2 = last - middle-1;//注意此处的长度int *L = new int[n1+1];int *R = new int[n2+1];int i , j ,k;for(i = 0;i<=n1;i++)L[i] = a[first+i];for(j = 0;j<=n2;j++)R[j] = a[middle+j+1];k = 0,i=0,j=0;while(i<=n1 &&j<=n2){if(L[i]<R[j])a[first+i+j] = L[i++];elsea[first+i+j] = R[j++];}while(i<=n1){a[first+i+j] = L[i++];}while(j<=n2){a[first+i+j] = R[j++];}delete []L;delete []R;}void mergesort(int *a, int first, int last) { if(first>=last)return; if(first<last){int mid = (first + last) / 2; mergesort(a, first, mid); //左边有序 mergesort(a, mid + 1, last); //右边有序 MergeArray(a, first, mid, last); //再将二个有序数列合并 }}void MergeSort(int *a, int n) { mergesort(a,0,n-1); } void PrintArray(int *b){for(int i = 0;i<10;i++){cout<<b[i]<<" ";}cout<<endl;}int main(){int a[10];for(int k = 0;k<5;k++){for(int i = 0;i<10;i++){a[i] = rand()%100;}cout<<"排序前:";PrintArray(a);//QSort(a,0,9);MergeSort(a,10);cout<<"排序后:";PrintArray(a);cout<<endl;}return 0;}
0 0
- 排序算法-归并排序
- 排序算法------归并排序
- 排序算法-归并排序
- 排序算法---归并排序
- 排序算法--归并排序
- 排序算法--归并排序
- 排序算法-归并排序
- 排序算法--归并排序
- 排序算法--归并排序
- 排序算法:归并排序
- 排序算法-归并排序
- 排序算法:归并排序
- 【排序算法】归并排序
- 排序算法--归并排序
- 排序算法-归并排序
- 排序算法--归并排序
- 排序算法:归并排序
- 【排序算法】归并排序
- 《深入理解Linux内核》软中断/tasklet/工作队列
- 233. Number of Digit One
- 关于项目启动jstl的jar包问题
- 淘淘商城SSM框架之异常处理篇
- 在select中,载入时默认select为空白,选项内不显示空白项
- 归并排序算法
- Combination Sum 题解
- generate_anchor.py运算解读
- Linux CentOS7安装Hadoop2.7集群
- IOS8以上版本,使用UIAlertController代替 UIActionSheet和UIAlertView
- 积累
- window环境下搭建ionic+cordova安卓app
- Java多线程基础学习之线程的创建方式总结
- 最全面的常用正则表达式大全