归并排序
来源:互联网 发布:水音铃 淘宝 编辑:程序博客网 时间:2024/06/03 20:30
归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用可以将A,B组各自再分成二组。依次类推,当分出来的小组只有一个数据时,可以认为这个小组组内已经达到了有序,然后再合并相邻的二个小组就可以了。这样通过先递归的分解数列,再合并数列就完成了归并排序
#include<iostream>#include<stdlib.h>using namespace std;void mergearray(int a[],int beg,int mid,int en,int* temp){ int i=beg; int j=mid+1; int k=0; while(i<=mid&&j<=en) { if(a[i]<a[j]) temp[k++]=a[i++]; else temp[k++]=a[j++]; } while(i<=mid) temp[k++]=a[i++]; while(j<=en) temp[k++]=a[j++]; i=0; while(i<k) { a[beg+i]=temp[i]; i++; }}void mergesort(int a[],int beg,int en,int temp[]){ if(beg<en) { //malloc只在main中初始化一次,减少开辟空间的时间浪费 mergesort(a,beg,(beg+en)/2,temp); mergesort(a,(beg+en)/2+1,en,temp); mergearray(a,beg,(beg+en)/2,en,temp); }}int main(){ int a[10]; //int b[4]={1,5,2,3}; for(int i=0;i<10;i++) a[i]=rand()%10; int *temp= (int*)malloc(sizeof(int)*10); //mergearray(b,0,1,3,temp); mergesort(a,0,9,temp); free(temp); for(int i=0;i<10;i++) cout<<a[i]<<' '; return 0;}
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- Java各类型转换以及序列化操作
- navicat
- SEO是什么,如何进行才高效!
- Fix the Problem with MergeList
- sscanf函数和正则表达式
- 归并排序
- wireshark过滤规则
- 在散碎时间中生活,在散碎时间中成长
- 程序员面试那些事儿(有亮点)
- iOS 静态库a文件的打包 中
- LeetCode: Search in Rotated Sorted Array II [081]
- 平面上最近点对
- 完全卸载oracle11g步骤
- http与https的区别