import java.io.*;
import java.util.*;
public class Sort {
public void QuickSort(int[] pData, int left, int right) {
int i, j;
int first, temp;
i = left;
j = right;
first = pData[left]; // 这里选其他的数也行,不过一般选第一个
// 一趟快速排序
while (true) {
// 从第二个数开始找大于中枢的数 ,从前面开始找大于pData[left]的数
while ((++i) < right - 1 && pData[i] < first)
;
// 从最后一个数开始找第一个小于中枢pData[left]的数
while ((--j) > left && pData[j] > first)
;
if (i >= j)
break;
// 交换两边找到的数
temp = pData[i];
pData[i] = pData[j];
pData[j] = temp;
}
// 交换中枢
pData[left] = pData[j];
pData[j] = first;
// 递归快排中枢左边的数据
if (left < j)
QuickSort(pData, left, j);
// 递归快排中枢右边的数据
if (right > i)
QuickSort(pData, i, right);
}
public static void main(String[] args) {
File file1 = new File("largeW.txt");
File file2 = new File("largeW_quick.txt");
ArrayList
list = new ArrayList
();
Sort sort = new Sort();
// 读取数据
try {
FileReader in = new FileReader(file1);
BufferedReader bufr = new BufferedReader(in);
String s = null;
int t;
while ((s = bufr.readLine()) != null) {
t = Integer.parseInt(s.trim());
list.add(t);
}
bufr.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
int[] a = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
a[i] = list.get(i);
}
long startsysDate = System.currentTimeMillis();
sort.QuickSort(a, 0, a.length);
long endsysDate = System.currentTimeMillis();
System.out.println(endsysDate - startsysDate);
try {
FileOutputStream fos = new FileOutputStream(file2);
OutputStreamWriter osw = new OutputStreamWriter(fos, "gb2312");
BufferedWriter bw = new BufferedWriter(osw);
String str;
for (int i = 0; i < list.size(); i++) {
str = Integer.toString(a[i]);
bw.write(str);
bw.newLine();
}
bw.close();
osw.close();
fos.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}