-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask1.java
91 lines (75 loc) · 2.49 KB
/
Task1.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* Kornilov Nikita, M3102, 25.09.2020 */
package Sem1.Lab2;
import java.io.*;
import java.util.*;
public class Task1 {
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public static void main(String[] args) {
new Task1().run("sort.in", "sort.out");
}
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens())
in = new StringTokenizer(br.readLine());
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public void solve() throws IOException {
int n = nextInt();
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
if (isNotSorted(array))
quickSort(array, 0, array.length - 1);
for (int i = 0; i < array.length - 1; i++) {
out.print(array[i]);
out.print(" ");
}
out.print(array[array.length - 1]);
}
public void quickSort(int[] array, int leftIndex, int rightIndex) {
if (leftIndex < rightIndex) {
int pivotIndex = partition(array, leftIndex, rightIndex);
quickSort(array, leftIndex, pivotIndex - 1);
quickSort(array, pivotIndex, rightIndex);
}
}
public boolean isNotSorted(int[] arr) {
for (int i = 0; i < arr.length - 1; i++)
if (arr[i] >= arr[i + 1])
return true;
return false;
}
private int partition(int[] array, int leftIndex, int rightIndex) {
Random random = new Random();
int pivotIndex = array[leftIndex + random.nextInt(rightIndex - leftIndex)];
while (leftIndex <= rightIndex) {
while (array[leftIndex] < pivotIndex)
leftIndex++;
while (array[rightIndex] > pivotIndex)
rightIndex--;
if (leftIndex <= rightIndex) {
int tmp = array[leftIndex];
array[leftIndex] = array[rightIndex];
array[rightIndex] = tmp;
leftIndex++;
rightIndex--;
}
}
return leftIndex;
}
public void run(String inputFile, String outputFile) {
try {
br = new BufferedReader(new FileReader(inputFile));
out = new PrintWriter(outputFile);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}