-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.java
More file actions
51 lines (33 loc) ยท 1.12 KB
/
test.java
File metadata and controls
51 lines (33 loc) ยท 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package algorithm.yjh;
import java.util.ArrayList;
import java.util.List;
import algorithm.custom.CustomList;
public class test {
public static void main(String[] args) {
List<Integer> n = CustomList.randomIntList();
System.out.println(quickSort(n).toString());
}
public static List<Integer> quickSort(List<Integer> n) {
// ์์ธ
if(n.size() < 2) {
return n;
}
int pivot = n.get(0); // ๊ธฐ์ค์ : ์ต์ ๊ฐ
List<Integer> lower = new ArrayList<Integer>(); // pivot๋ณด๋ค ๋ฎ์ ๊ฐ
List<Integer> higher = new ArrayList<Integer>(); // pivot๋ณด๋ค ํฐ ๊ฐ
for(int i = 1; i < n.size(); i++) {
if(n.get(i) < pivot) {
lower.add(n.get(i)); // add๋ ๋ฌด์กฐ๊ฑด ๋ค์ ๋ถ์
} else {
higher.add(n.get(i));
}
}
// lower๋ฆฌ์คํธ๊ฐ ์ ๋ ฌ ๋ ๋๊น์ง ๋ถํ ํด์ ๊ฐ์ ์ถ๊ฐ
List<Integer> result = quickSort(lower); // ์ฌ๊ท
// pivot๊ฐ (์ค๊ฐ๊ฐ) ์ถ๊ฐ
result.add(pivot);
// higher ๋ฆฌ์คํธ๊ฐ ์ ๋ ฌ์ด ๋ ๋๊น์ง ๋ถํ ํด์ ๊ฐ์ ์ถ๊ฐ
result.addAll(quickSort(higher)); // addAll(Collection) ๋ฆฌ์คํธ ์ ์ฒด ์ถ๊ฐ
return result;
}
}