## Stay hungry, stay foolish !

I've failed over and over and over again in my life. And that is why I succeed.

# 算法导论学习2.3 分治法 (这个递归终于懂了。。)

import java.util.Arrays;

public class MergeSort {

public static void main(String[] args) {

int[] data = {2,4,0,7,1,8,3,6};

sort(data, 0, data.length - 1);

for(int i = 0; i < data.length; i++) {
System.out.print(data[i] + " ");
}
}

public static void sort(int[] data, int p, int r) {

if (p < r) {
int q = (p + r) / 2;
sort(data, p, q);
sort(data, q + 1, r);
merge(data, p, q, r);
}
}

private static void merge(int[] data, int p, int q, int r) {
int[] left = Arrays.copyOfRange(data, p, q + 1);

int[] right = Arrays.copyOfRange(data, q + 1, r + 1);

int i = 0;
int j = 0;
int k = 0;
while (k < r - p + 1) {
if (i == left.length) {
data[p + k] = right[j++];
}
else if (j == right.length) {
data[p + k] = left[i++];
}
else if (left[i] < right[j]) {
data[p + k] = left[i++];
}
else {
data[p + k] = right[j++];
}
k++;
}
}
}  

//  测试的代码
import java.util.Arrays;

public class MergeSort {

public static void main(String[] args) {

int[] data = {1,3,4,2};

int[] test = new int[data.length];

sort(data, 0, data.length - 1, test);

System.out.print("out:\t");
for(int i = 0; i < data.length; i++) {
System.out.print(data[i] + " ");
}
}

public static void sort(int[] data, int p, int r, int[] test) {

if (p < r) {
int q = (p + r) / 2;
sort(data, p, q, test);

//            System.out.print("sort1.\t");
//            for(int i = 0; i < test.length; i++) {
//        		System.out.print(test[i] + " ");
//        	}
//            System.out.println();

sort(data, q + 1, r, test);

//            System.out.print("sort2.\t");
//            for(int i = 0; i < test.length; i++) {
//        		System.out.print(test[i] + " ");
//        	}
//            System.out.println();

merge(data, p, q, r, test);

//            System.out.print("merge.\t");
//            for(int i = 0; i < test.length; i++) {
//        		System.out.print(test[i] + " ");
//        	}
//            System.out.println();

}
}

private static void merge(int[] data, int p, int q, int r, int[] test) {
int[] left = Arrays.copyOfRange(data, p, q + 1);

int[] right = Arrays.copyOfRange(data, q + 1, r + 1);

int i = 0;
int j = 0;
int k = 0;
while (k < r - p + 1) {
if (i == left.length) {
data[p + k] = right[j++];
test[p + k] = data[p + k];
}
else if (j == right.length) {
data[p + k] = left[i++];
test[p + k] = data[p + k];
}
else if (left[i] < right[j]) {
data[p + k] = left[i++];
test[p + k] = data[p + k];
}
else {
data[p + k] = right[j++];
test[p + k] = data[p + k];
}
k++;
}
}
}


sort1.	0 0 0 0
sort2.	0 0 0 0
merge.	1 3 0 0
sort1.	1 3 0 0
sort1.	1 3 0 0
sort2.	1 3 0 0
merge.	1 3 2 4
sort2.	1 3 2 4
merge.	1 2 3 4
out:	1 2 3 4 

#### 算法导论学习之分治法

2017-04-17 22:17:28

#### 算法导论 2.3-4

2013-05-07 20:20:44

#### MIT算法导论学习笔记-Lecture3：分治法

2014-07-25 09:01:04

#### 《算法导论》学习心得（一）——分治求最大子数问题

2014-10-29 19:54:24

#### 算法导论-分治法-最近点对-HDOJ1007

2014-03-20 10:38:51

#### 《算法导论》读书笔记（二）——分治法

2013-09-02 22:15:38

#### 中科大研究生算法导论课件

2011年05月20日 3.3MB 下载

#### 【算法导论】【笔记】【分治法】最近点对问题

2017-12-26 16:30:15

#### 算法导论之2-3思考题

2015-10-24 19:15:37

#### 【从零学习经典算法系列】分治与递归1——递归表达式与解法初步

2014-07-11 09:13:32