归并排序
来源:互联网 发布:ins注册提示网络错误 编辑:程序博客网 时间:2024/05/19 03:44
/*¹é²¢ÅÅÐò*/#include <iostream>using namespace std;void mergeall(int a[],int first,int mid,int end,int t[]){int i = first;int j = mid+1;int k =0;while(i<= mid && j <= end){if(a[i] <= a[j]){t[k++] = a[i++];}else{t[k++] = a[j++];}}while(j <= end){t[k++] = a[j++]; }while(i <= mid){t[k++] = a[i++];}for(int m = 0;m <k;m++){a[first+m] = t[m];}}void merge(int a[],int first,int end,int t[]){int mid = (first+end)/2;if(first<end){merge(a,first,mid,t);merge(a,mid+1,end,t);mergeall(a,first,mid,end,t);}}void mergefirst(int a[],int first,int end){int * t = new int[8];merge(a,first,end,t);delete []t;}void main(){int a[8] = {3,1,4,2,6,7,9,8};mergefirst(a,0,7);for(int i =0;i<8;i++){cout << a[i];}}
遇到的问题:
一开始没有mergefirst函数,直接在merge函数中new了一个动态数组,但是会导致debug error;
难点:
在于mergeall函数,采用直接插入的方法,复杂度为O(n).
参考:http://blog.csdn.net/morewindows/article/details/6678165
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- OpenCV从摄像头读取视频
- httpSession的正确理解
- 3S CoDeSys运行时工具包空指针引用
- PAT: 1054. The Dominant Color (20)
- 京东招股书披露:微信价值10亿美元
- 归并排序
- C++通过occi执行select语句、存储过程、函数,取cursor值示例
- /dev/tty
- Android的四大组件之二--BroadcastReceiver(相关内容,开机启动Service)
- 仿jdk的ArrayList实现
- 黑马程序-常见异常对象
- 苹果产品判断
- Intent中的四个重要属性——Action、Data、Category、Extras
- 全志A20[android教程]-红外遥控器调试