-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuickSort.java
64 lines (57 loc) · 1.92 KB
/
QuickSort.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
import java.io.*;
import java.util.StringTokenizer;
public class QuickSort {
static final int file_size = 1000000;
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new FileReader("./Random/"+file_size+".txt"));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("./201302482_quick"));
int[] array = new int[file_size];
int count = 0;
String str = null;
while((str = bufferedReader.readLine()) != null){
StringTokenizer stringTokenizer = new StringTokenizer(str);
while(stringTokenizer.hasMoreTokens()){
array[count++] = Integer.parseInt(stringTokenizer.nextToken());
}
}
long start = System.currentTimeMillis();
quickSort(array, 0, file_size-1);
long end = System.currentTimeMillis();
for(int i=0; i<file_size-1; i++){
bufferedWriter.write(array[i]+" ");
}
bufferedWriter.write(array[file_size-1]+"");
bufferedWriter.flush();
System.out.println("실행 시간 : "+(end - start));
}
public static void quickSort(int[] A, int left, int right){
if(left < right){
int pivot = partition(A,left,right);
quickSort(A, left, pivot-1);
quickSort(A, pivot+1, right);
}
}
public static int partition(int[] A, int left, int right){
int pivot = A[right];
int i = left;
int j = right-1;
while(j != i-1){
if(pivot >= A[i])
i++;
else if(pivot < A[j])
j--;
else{
swap(A, i, j);
i++;
j--;
}
}
swap(A, i, right);
return i;
}
private static void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}