From 33dae6dc891b26d53381623dd5906082e7dda0f6 Mon Sep 17 00:00:00 2001 From: mahu8766 Date: Mon, 3 Sep 2018 16:07:56 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E7=AE=97=E6=B3=95.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- notes/算法.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/notes/算法.md b/notes/算法.md index cb83830b..5f025409 100644 --- a/notes/算法.md +++ b/notes/算法.md @@ -421,6 +421,56 @@ public class Shell> extends Sort { ### 1. 归并方法 +package sortdemo; + +import java.util.Arrays; + +/** + * Created by chengxiao on 2016/12/8. + */ +public class MergeSort { + public static void main(String []args){ + int []arr = {9,8,7,6,5,4,3,2,1}; + sort(arr); + System.out.println(Arrays.toString(arr)); + } + public static void sort(int []arr){ + int []temp = new int[arr.length];//在排序前,先建好一个长度等于原数组长度的临时数组,避免递归中频繁开辟空间 + sort(arr,0,arr.length-1,temp); + } + private static void sort(int[] arr,int left,int right,int []temp){ + if(left