package Sort;

import java.util.Arrays;

public class MergeSort {

    public static void merge(int[] list, int low, int mid, int high) {

        int[] temp = new int[high - low + 1];
int i = low;
int j = mid + 1;
int k = 0; while (i <= mid && j <= high) {
if (list[i] < list[j]) {
temp[k++] = list[i++];
} else {
temp[k++] = list[j++];
}
} while (i <= mid) {
temp[k++] = list[i++];
} while (j <= high) {
temp[k++] = list[j++];
} for (int k2 = 0; k2 < temp.length; k2++) {
list[k2 + low] = temp[k2];
} } public static int[] sort(int[] list, int low, int high) {
int mid = (low + high) / 2;
if (low < high) {
sort(list, low, mid);
sort(list, mid + 1, high);
merge(list, low, mid, high);
} return list;
} public static void main(String[] args) {
int[] list = {5,4,3,2,1,6,8,7};
System.out.println(Arrays.toString(sort(list,0,list.length-1)));
} }

Java实现归并排序的更多相关文章

  1. Java实现归并排序(转)

    Java实现归并排序  本文转自https://www.cnblogs.com/of-fanruice/p/7678801.html 归并排序 (merge sort) 是一类与插入排序.交换排序.选 ...

  2. 【Java】 归并排序的非递归实现

    归并排序可以采用递归方法(见:归并排序),但递归方法会消耗深度位O(longn)的栈空间,使用归并排序时,应该尽量使用非递归方法.本文实现了java版的非递归归并排序. 更多:数据结构与算法合集 思路 ...

  3. Java算法-归并排序

    归并排序采用的是递归来实现,属于“分而治之”,将目标数组从中间一分为二,之后分别对这两个数组进行排序,排序完毕之后再将排好序的两个数组“归并”到一起,归并排序最重要的也就是这个“归并”的过程,归并的过 ...

  4. Java实现归并排序和快速排序

    参考http://blog.csdn.net/morewindows/article/details/6684558和http://developer.51cto.com/art/201206/344 ...

  5. java实现归并排序算法

    归并排序算法思想:分而治之(divide - conquer);每个递归过程涉及三个步骤第一, 分解: 把待排序的 n 个元素的序列分解成两个子序列, 每个子序列包括 n/2 个元素.第二, 治理: ...

  6. java泛型中使用的排序算法——归并排序及分析

    一.引言 我们知道,java中泛型排序使用归并排序或TimSort.归并排序以O(NlogN)最坏时间运行,下面我们分析归并排序过程及分析证明时间复杂度:也会简述为什么java选择归并排序作为泛型的排 ...

  7. java算法----排序----(5)归并排序

    package log; import java.util.Arrays; public class Test4 { /** * java算法---归并排序 * * @param args */ pu ...

  8. Java数据结构和算法 - 递归

    三角数字 Q: 什么是三角数字? A: 据说一群在毕达哥拉斯领导下工作的古希腊的数学家,发现了在数学序列1,3,6,10,15,21,……中有一种奇特的联系.这个数列中的第N项是由第N-1项加N得到的 ...

  9. 常用算法之排序(Java)

    一.常用算法(Java实现) 1.选择排序(初级算法) 原理:有N个数据则外循环就遍历N次并进行N次交换.内循环实现将外循环当前的索引i元素与索引大于i的所有元素进行比较找到最小元素索引,然后外循环进 ...

随机推荐

  1. linux内核打印数据到串口控制台,printk数据不打印问题

    linux内核打印数据到串口控制台问题 原文来源:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.查看当前控制台的打印级别 cat /proc/sys/kern ...

  2. android中设置控件获得焦点 (转)

    android中,要使控件获得焦点,需要先setFocus,再requestFocus. 以Button为例:                 btn.setFocusable(true);      ...

  3. NSURLConnection 异步加载网络数据

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  4. 注解@RequestMapping 的使用

    1首先@RequestMapping 中的值,我们说请求方法l路径,请求url我们都知道怎么请求了,在第一节helloworld中, 我们先说我们先建一个类,RequestMappingTest 方法 ...

  5. ViewPager打造轮播图(Banner)\引导页(Guide)

    今年7月时,在Github发布了一个开源的Banner库,虽然Star不多,但还是有少部分人使用. Banner效果:  昨天,有使用此库的同学提出需求,想在引导页的时候用这个库并且最后一页有进入按钮 ...

  6. sublime text3点击ctrl+B无法运行Python程序?

    1.打开sublime text 3 ,选择 tools-->Build System-->New Build System.... 2.将下面代码块复制进新文件中,并命名为Python. ...

  7. Codeforces Round B. Buttons

    Manao is trying to open a rather challenging lock. The lock has n buttons on it and to open it, you ...

  8. [Leetcode] Decode Ways

    A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...

  9. C#_Express-ickd接口

    爱查快递接口使用 using System; using System.Collections.Generic; using System.IO; using System.Net; using Sy ...

  10. Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...