Skip to content

Commit 39ff61b

Browse files
committed
🍱 exercise;
1 parent 790d899 commit 39ff61b

File tree

4 files changed

+78
-2
lines changed

4 files changed

+78
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package me.codebase.leetcode.algo;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by chendong on 2018/1/12.
7+
*/
8+
public class MergeSort implements Sort {
9+
10+
11+
public static void main(String[] args) {
12+
// sort(arr);
13+
System.out.println(Arrays.toString(new MergeSort().sort(arr)));
14+
}
15+
16+
/* public int[] sort(int[] a){
17+
int[] h = a.clone();
18+
return a;
19+
}*/
20+
21+
22+
23+
public int[] sort(int[] a) {
24+
int[] helper = a.clone();
25+
sort(a, 0, a.length - 1, helper);
26+
return arr;
27+
}
28+
29+
private static void sort(int[] a, int lo, int hi, int[] helper) {
30+
if (lo >= hi) return;
31+
int mid = (lo + hi) / 2;
32+
sort(a, lo, mid, helper);
33+
sort(a, mid + 1, hi, helper);
34+
merge(a, lo, mid, hi, helper);
35+
}
36+
37+
private static void merge(int[] a, int lo, int mid, int hi, int[] helper) {
38+
39+
System.arraycopy(a, lo, helper, lo, hi + 1 - lo);
40+
int i = lo;
41+
int j = mid + 1;
42+
for (int k = lo; k <= hi; k++) {
43+
if (i > mid)
44+
a[k] = helper[j++];
45+
else if (j > hi)
46+
a[k] = helper[i++];
47+
else if (helper[i] <= helper[j])
48+
a[k] = helper[i++];
49+
else
50+
a[k] = helper[j++];
51+
}
52+
53+
}
54+
}

src/me/codebase/leetcode/algo/QuickSort.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
/**
66
* Created by chendong on 2018/1/11.
77
*/
8-
public class QuickSort {
8+
public class QuickSort implements Sort{
99

10-
private static final int[] arr = {0, 2, 4, 6, 8, 5, 3, 7, 9, 1, 400};
1110

1211
public static void main(String[] args) {
1312
System.out.println(Arrays.toString(new QuickSort().sort(arr)));
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package me.codebase.leetcode.algo;
2+
3+
/**
4+
* Created by chendong on 2018/1/12.
5+
*/
6+
public interface Sort {
7+
8+
int[] arr = {0, 2, 4, 6, 8, 5, 3, 7, 9, 1, 400};
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package me.codebase.leetcode.algo;
2+
3+
import me.codebase.leetcode.structure.TreeNode;
4+
5+
/**
6+
* Created by chendong on 2018/1/12.
7+
*/
8+
public class TreeBuilder {
9+
10+
public static TreeNode getRootNode(){
11+
return null;
12+
}
13+
}

0 commit comments

Comments
 (0)