归并排序
来源:互联网 发布:mysql 查看使用连接数 编辑:程序博客网 时间:2024/05/22 11:45
import java.math.* ;import java.util.* ;/** * */public class Test extends Base{public static void main(String[] args) throws Exception{int[] arr = getRandomArr() ;println(arr); int[] temp = new int[arr.length] ;mergeSort(arr , arr.length);println(arr);println();}public static void mergeSort(int[] arr , int n) throws Exception{int[] temp = new int[n] ;divideSort(arr, 0 , n - 1 , temp);}public static void divideSort(int[]arr , int first , int last , int[] temp) throws Exception{if(first < last){divideSort(arr , first, (first + last) / 2 , temp);divideSort(arr , (first + last) / 2 + 1 , last , temp);mergeArray(arr, first , (first + last) / 2 , last , temp);}}public static void mergeArray(int[] arr ,int first, int mid , int last , int[] temp){int firstStart = first , firstEnd = mid ;int secondStart = mid + 1 , secondEnd = last ;int k = 0 ;while (firstStart <= firstEnd && secondStart <= secondEnd){if(arr[firstStart] < arr[secondStart])temp[k++] = arr[firstStart++] ;else temp[k++] = arr[secondStart++] ;}while(firstStart <= mid)temp[k++] = arr[firstStart++] ;while(secondStart <= last)temp[k++] = arr[secondStart++] ;for (int i = 0; i < k ; i++ ){arr[first + i] = temp[i] ;}}}
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 正向代理 反向代理 CDN
- Leetcode 187 Repeated DNA Sequences 重复出现的DNA序列
- Num 19: 最大公约数.最小公倍数.素数的判断
- hadoop分布式集群搭建
- 南邮 OJ 1446 嘉娃的难题
- 归并排序
- Directory 类
- Android SharedPreferences使用以及原理详解
- Rescue(BFS) HDU 1242
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- iOS之友盟错误统计解决
- 生成framework ,脚本
- Android adb的一些使用方法
- Android项目Tab类型主界面大总结