归并排序
来源:互联网 发布:女生一开始很热情知乎 编辑:程序博客网 时间:2024/06/05 07:38
package com.lin.test;public class Sort {static void merge(int a[], int first, int mid, int last, int[] temp) {int i = first, j = mid + 1, k = 0;while (i <= mid && j <= last) {if (a[i] <= a[j]) {temp[k++] = a[i++];} else {temp[k++] = a[j++];}}while (i <= mid) {temp[k++] = a[i++];}while (j <= last) {temp[k++] = a[j++];}for (i = 0; i < k; i++) {a[first + i] = temp[i];}}static void mergeSort(int a[], int first, int last, int temp[]) {if (first < last) {int mid = (first + last) / 2;mergeSort(a, first, mid, temp);mergeSort(a, mid + 1, last, temp);merge(a, first, mid, last, temp);}}public static void main(String[] args) {int a[] = { 3, 2, 5, 7, 8, 9, 4, 1, 0, 4, 6 };int[] temp = new int[a.length];mergeSort(a, 0, a.length - 1, temp);for (int i : a) {System.out.print(i + " ");}}}
0 0
- 归并排序-归并排序
- 归并和归并排序
- 归并与归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 排序::归并
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 归并排序
- 解决:The APR based Apache Tomcat Native library
- MySQL架构组成之物理文件组成
- POJ 1573 & POJ 2632(两道有趣的Robot)
- C语言函数返回多个值的方法
- 立夏时节话评弹
- 归并排序
- 面对GC的java编程
- Android编译过程详解(一)
- HNOI 2008 水平可见直线
- 马云:依旧是阿里巴巴的核心人物和精神领袖
- JS验证
- 关于不能连接SQL server的问题
- poj1305
- MSVC vs. MinGW 之 (lib,dll,def,obj,exe) vs (a,dll,def,o,exe) 玩转攻略手记