归并排序
来源:互联网 发布:韩顺平php视频教程 编辑:程序博客网 时间:2024/06/07 05:07
public class TestMergeSort{ public static void merge(int[] a, int low, int mid, int high) { int[] temp = new int[high - low + 1]; int i = low;//left pointer int j = mid + 1;//right pointer int k = 0; //move smaller element to new array while (i <= mid && j <= high) { if (a[i] < a[j]) { temp[k++] = a[i++]; } else { temp[k++] = a[j++]; } } // left on left side while (i <= mid) { temp[k++] = a[i++]; } //left on right while (j <= high) { temp[k++] = a[j++]; } //copy to old array for (int k2 = 0; k2 < temp.length; k2++) { a[k2 + low] = temp[k2]; } } static void mergeSort(int[] r, int low, int high) { if (low < high) { int mid = (low + high) / 2; mergeSort(r, low, mid); mergeSort(r, mid + 1, high); merge(r, low, mid, high); } } public static void display(int[] R) { System.out.println(); for (int i = 0; i < R.length; i++) { System.out.print(R[i] + " "); } } public static void main(String[] args) { int[] R = { 71, 88, 93, 77, 5, 16, 63, 74, 89, 18 }; display(R); mergeSort(R, 0, R.length - 1); display(R); }}
阅读全文
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- oracle中LPAD和RPAD函数的用法
- java学习笔记(计算机基础知识)
- Java基础知识
- hdu 2082 找单词
- hdu5831
- 归并排序
- mysql中的inner join,left join,right join
- MySQL常用性能分析方法-profile,explain,索引
- 外链接
- docker(16):使用 pinpoint 进行微服务数据统计
- day07(创建对象,static)
- io流读取文件内容并将其逆值输出到控制台
- 畅通工程再续 HDU 1875
- hdu 1085 Holding Bin-Laden Captive!