常用排序算法之归并排序
来源:互联网 发布:人工智能手抄报资料 编辑:程序博客网 时间:2024/06/07 05:06
常用排序算法之冒泡排序
常用排序算法之快速排序
常用排序算法之插入排序
常用排序算法之希尔排序
常用排序算法之选择排序
常用排序算法之堆排序
归并排序算法
/** * 归并排序 * @param array * @return */ public static int[] mergingSort(int[] array) { sort(array, 0, array.length - 1); return array; } public static void sort(int[] data, int left, int right) { if (left < right) { //找出中间索引 int center = (left + right) / 2; //对左边数组进行递归 sort(data, left, center); //对右边数组进行递归 sort(data, center + 1, right); merge(data, left, center, right); } } public static void merge(int[] data, int left, int center, int right) { int[] tmpArr = new int[data.length]; int mid = center + 1; int third = left; int tmp = left; while (left <= center && mid <= right) { if (data[left] <= data[mid]) { tmpArr[third++] = data[left++]; } else { tmpArr[third++] = data[mid++]; } } while (mid <= right) { tmpArr[third++] = data[mid++]; } while (left <= center) { tmpArr[third++] = data[left++]; } while (tmp <= right) { data[tmp] = tmpArr[tmp++]; } }
运行
/** * 待排序的数组 */ private static int[] array = { 12, 10, 5, 9, 5, 32, 16, 1, 9, 99, 80, 3, 18, 19, 20, 25, 7, 15 }; public static void main(String[] args) { int[] result = SortUtils.mergingSort(array.clone()); System.out.println(Arrays.toString(result)); }
输出
[1, 3, 5, 5, 7, 9, 9, 10, 12, 15, 16, 18, 19, 20, 25, 32, 80, 99]
常用算法时间复杂度和空间复杂度表
0 0
- 常用排序算法之归并排序
- 常用排序算法之归并排序
- 常用排序算法之归并排序
- 常用排序算法之归并排序
- 常用排序算法--归并排序
- 常用排序算法-归并排序
- Java常用算法之归并排序
- 常用算法之排序算法四【归并排序】
- 排序算法之归并排序
- 排序算法之归并排序
- 排序算法之归并排序
- 排序算法之归并排序
- 排序算法之归并排序
- 排序算法之归并排序
- 排序算法之归并排序
- 排序算法之归并排序
- 排序算法之归并排序
- 排序算法之归并排序
- Web容器与Servlet
- 【转】Win32下的socket编程
- PAT-B 1023. 组个最小数 (20)
- MPAndroidChart使用二之折线图
- 排序_2.快速排序
- 常用排序算法之归并排序
- HDU1501 Zipper(DFS)
- 多线程实现(Thread类)
- 网络虚拟化之FlowVisor:网络虚拟层(下)
- 全局变量、局部变量、全局静态变量、局部静态变量的区别
- tjut 4941
- yum方式安装MySQL
- MPAndroidChart使用三之柱形图
- POJ2403 Hay Points