Skip to content

Commit 790d899

Browse files
committed
🍱 Adding or updating assets. add QuickSort Exc
1 parent 7a98439 commit 790d899

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

src/me/codebase/leetcode/_543.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.codebase.leetcode;
22

3+
import me.codebase.leetcode.structure.TreeNode;
4+
35
/**
46
* Created by chendong on 2018/1/10.
57
*/
@@ -8,4 +10,11 @@ public class _543 {
810
public static void main(String[] args) {
911

1012
}
13+
14+
15+
private static TreeNode getTree(){
16+
17+
return new TreeNode(1);
18+
19+
}
1120
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package me.codebase.leetcode.algo;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by chendong on 2018/1/11.
7+
*/
8+
public class QuickSort {
9+
10+
private static final int[] arr = {0, 2, 4, 6, 8, 5, 3, 7, 9, 1, 400};
11+
12+
public static void main(String[] args) {
13+
System.out.println(Arrays.toString(new QuickSort().sort(arr)));
14+
}
15+
16+
public int[] sort(int[] arr) {
17+
sort(0, arr.length - 1, arr);
18+
return arr;
19+
}
20+
21+
public void sort(int left, int right, int[] arr) {
22+
if (right > left) {
23+
int mid = partition(left, right, arr);
24+
sort(left, mid - 1, arr);
25+
sort(mid + 1, right, arr);
26+
}
27+
}
28+
29+
private int partition(int left, int right, int[] arr) {
30+
int mid = (left + right) / 2;
31+
swap(mid, right, arr);
32+
int index = left;
33+
for (int i = left; i < right; i++) {
34+
if (arr[i] < arr[right]) {
35+
swap(i, index, arr);
36+
index++;
37+
}
38+
}
39+
swap(index, right, arr);
40+
return index;
41+
}
42+
43+
private void swap(int i, int j, int[] arr) {
44+
int temp = arr[i];
45+
arr[i] = arr[j];
46+
arr[j] = temp;
47+
}
48+
}

src/me/codebase/leetcode/structure/TreeNode.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*/
66
public class TreeNode {
77
int val;
8-
TreeNode left;
9-
TreeNode right;
8+
public TreeNode left;
9+
public TreeNode right;
1010

11-
TreeNode(int x) {
11+
public TreeNode(int x) {
1212
val = x;
1313
}
1414
}

0 commit comments

Comments
 (0)