-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution3.java
More file actions
60 lines (55 loc) · 1.63 KB
/
Solution3.java
File metadata and controls
60 lines (55 loc) · 1.63 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
57
58
59
60
import java.io.*;
import java.math.*;
import java.util.*;
public class Solution3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] sorted = new String[n];
for(int sorted_i=0; sorted_i < n; sorted_i++){
sorted[sorted_i] = in.next();
}
BigInteger[] unsorted = new BigInteger[n];
for(int unsorted_i=0; unsorted_i < n; unsorted_i++){
unsorted[unsorted_i] = new BigInteger(sorted[unsorted_i]);
}
quickSort(0, unsorted.length -1, unsorted);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out),32768);
try {
for (int i = 0; i < unsorted.length; i++) {
//System.out.println(unsorted[i]);
out.write(unsorted[i] + "\n");
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void quickSort(int lowerIndex, int higherIndex, BigInteger[] array) {
int i = lowerIndex;
int j = higherIndex;
BigInteger pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
while (i <= j) {
while (array[i].compareTo(pivot) == -1) {
i++;
}
while (array[j].compareTo(pivot) == 1) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j, array);
i++;
j--;
}
}
if (lowerIndex < j)
quickSort(lowerIndex, j, array);
if (i < higherIndex)
quickSort(i, higherIndex, array);
}
private static void exchangeNumbers(int i, int j, BigInteger[] array) {
BigInteger temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}