-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.java
More file actions
42 lines (35 loc) ยท 1.44 KB
/
test.java
File metadata and controls
42 lines (35 loc) ยท 1.44 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
package algorithm.lsh;
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); // ๊ธฐ์ค์ ๊ฐ์ฅ ๋น ๋ฅธ ๊ฐ์ ์ง์
// pivot ๋ณด๋ค ๋ฎ์๊ฐ
List<Integer> lower = new ArrayList<Integer>();
// pivot ๋ณด๋ค ๋์๊ฐ
List<Integer> higher = new ArrayList<Integer>();
// i ๋ n[0] ์ด pivot ์ผ๋ก ์ ํด์ ธ ์์์ผ๋ก 1๋ถํฐ
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);
result.add(pivot); // higher ๋ pivot ๋ณด๋ค ํฌ๊ณ lower ๋ ์์์ผ๋ก ๊ทธ ์ค๊ฐ์ add ๋ก ์์ํ๋๋ง์ ์ง์ด๋ฃ์ด pivot ์ ์์น๋ฅผ ์ ํด์ค๋ค
//List<Integer> result = quickSort(higher); => ํ๋ฆผx
result.addAll(quickSort(higher)); // ๋ง๋ ํํ
// higher ๋ฆฌ์คํธ๊ฐ ์ ๋ ฌ์ด ๋ ๋๊น์ง ๋ถํ ํด์ ๊ฐ์ ์ถ๊ฐํ๋ค.
return result;
}
}