-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
47 lines (43 loc) · 1.21 KB
/
Solution.java
File metadata and controls
47 lines (43 loc) · 1.21 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
import java.util.*;
import java.math.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String[] unsorted = new String[n];
for(int unsorted_i=0; unsorted_i < n; unsorted_i++){
unsorted[unsorted_i] = in.next();
}
quickSort(0, unsorted.length -1, unsorted);
for (int i = 0; i < unsorted.length; i++) {
System.out.println(unsorted[i]);
}
}
public static void quickSort(int lowerIndex, int higherIndex, String[] array) {
int i = lowerIndex;
int j = higherIndex;
BigInteger pivot = new BigInteger(array[lowerIndex+(higherIndex-lowerIndex)/2]);
while (i <= j) {
while (new BigInteger(array[i]).compareTo(pivot) == -1) {
i++;
}
while (new BigInteger(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, String[] array) {
String temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}