-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ2750.java
More file actions
56 lines (42 loc) · 1.22 KB
/
BJ2750.java
File metadata and controls
56 lines (42 loc) · 1.22 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
49
50
51
52
53
54
55
56
package javaBackjoon;
import java.util.Scanner;
import java.util.ArrayList;
public class BJ2750 {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
ArrayList<Integer> inputAry = new ArrayList<Integer>();
int size = input.nextInt();
for(int i = 0 ; i < size; i++) {
inputAry.add(input.nextInt());
}
quickSortAry(inputAry, 0, inputAry.size() - 1);
for(int value : inputAry) {
System.out.println(value);
}
}
static void quickSortAry(ArrayList<Integer> ary, int leftIdx, int rightIdx) {
if(leftIdx < rightIdx) {
int pivotIdx = quickSortDivide(ary, leftIdx, rightIdx);
if(leftIdx < pivotIdx) quickSortAry(ary, leftIdx, pivotIdx - 1);
if(pivotIdx + 1 < rightIdx) quickSortAry(ary, pivotIdx + 1, rightIdx);
}
}
static int quickSortDivide(ArrayList<Integer> ary, int left, int right) {
int idxL = left + 1;
int pivotIdx = left;
int pivot = ary.get(pivotIdx);
int tmp;
while(idxL <= right) {
if(ary.get(idxL) < pivot) {
pivotIdx++;
tmp = ary.get(idxL);
ary.set(idxL, ary.get(pivotIdx));
ary.set(pivotIdx, tmp);
}
idxL++;
}
ary.set(left, ary.get(pivotIdx));
ary.set(pivotIdx, pivot);
return pivotIdx;
}
}