归并排序
来源:互联网 发布:mysql循环执行sql语句 编辑:程序博客网 时间:2024/05/14 21:56
排序过程:将数组不断的分割,最终分成只有两个数的序列,然后对这些序列进行排序,再将这些序列进行合并
public void sorting() {mergeSorting(0,array.length-1);}private void mergeSorting(int low,int high){if(low<high){int mid=(low+high)/2;mergeSorting(low,mid);mergeSorting(mid+1,high);merge(low,high);}}//两个数组合并private void merge(int low,int high){int mid=(high+low)/2;int[] temp=new int[high-low+1];int k=0,i=0,j=0;for(i=low,j=mid+1;i<=mid&&j<=high;){if(array[i]<=array[j]){temp[k++]=array[i++];}else if(array[i]>array[j]){temp[k++]=array[j++];}}//循环结束,如果还有没有复制完的数据while(i<=mid){temp[k++]=array[i++];}while(j<=high){temp[k++]=array[j++];}for(i=low;i<=high;i++){array[i]=temp[i-low];}}
时间复杂度: 平均O(nlogn) 最好O(nlogn) 最坏O(nlogn) 稳定
代码下载:
http://download.csdn.net/detail/hbdatouerzi/9330395
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- apache 虚拟主机详解 有图哦
- jvisualvm监控tomcat
- 两阶段提交事务
- nsa ant tools
- php+apc 缓存
- 归并排序
- 蓝桥杯 题目1 最大最小值(排序)
- 差值
- htop
- CodeIgniter框架入门教程-HelloWorld
- php+apc 安装与优化
- Java JDBC封装模式
- 《PHP+MySQL八大动态Web应用实践》源码修正及分享
- CSDN博客第一篇开启