归并排序
来源:互联网 发布:mac上如何装office 编辑:程序博客网 时间:2024/05/22 08:12
将两个有序的数组合成一个有序的组合,时间复杂度为O(nlogn)
将一个数组,分成两个部分,每一个部分接着分两个部分,直到只有一个元素,右边一样,然后将相邻的二个部分组合在一块,直到所有的都合并。先分解,在合并。
public static void mergeSort(int a[],int first,int last,int temp[]){ if(a==null||first<0||last<0){ return; } if(first<last){ int mid=(first+last)>>1; mergeSort(a, first, mid, temp); mergeSort(a, mid+1, last, temp); mergeArray(a, first, mid, last, temp); } } public static void mergeArray(int a[],int first,int mid,int last,int temp[]){ if(a==null||first<0||last<0||mid<0){ return; } int i=first,j=mid+1; int m=mid,n=last; int k=0; while(i<=m&&j<=n){ if(a[i]<a[j]){ temp[k++]=a[i++]; } else{ temp[k++]=a[j++]; } } while(i<=m){ temp[k++]=a[i++]; } while(j<n){ temp[k++]=a[j++]; } for(i=0;i<k;i++){ a[first+i]=temp[i]; } }
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- day17/ArraysDemo1.java
- Java Eclipse更改皮肤
- java调用c(c++)写dll文件
- linux开发环境搭建-----虚拟机和windows的网络连接原理+具体配置。
- day17/CollectionsDemo1.java
- 归并排序
- cocos2dx 常见的22种特效 3D特效
- day17/CollectionsDemo2.java
- 剑指offer 面试题16—反转链表
- vb.net环境下将DataTable转成泛型集合方法
- day17/CollectionsDemo3.java
- Java基础——多线程+卖票程序+同步函数-单例设计模式+死锁
- ACDREAM 03C Robbers(贪心专场)
- 黑马程序员---------------JAVA基础--------正则表达式