快速排序

来源:互联网 发布:更改防火墙端口 编辑:程序博客网 时间:2024/06/06 09:59
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class KuaisuSort {
public static void main(String[] args) throws IOException {
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String []s=in.readLine().trim().split(" ");
int a[]=new int[s.length];
for (int i = 0; i < s.length; i++) {
a[i]=Integer.parseInt(s[i]);
}
quick(a);
for (int i: a) {
System.out.print(i+" ");
}
System.out.println();
}


private static void quick(int[] a) {
// TODO Auto-generated method stub
if (a.length>0) {
quickSort(a, 0,a.length-1);
}
}


public static int getMiddle(int[] a,int low, int high) {
// TODO Auto-generated method stub
int temp=a[low];
while (low<high) {
if (low<high && a[high]>temp) {
high--;
}
a[low]=a[high];
while (low<high && a[low]<temp) {
low++;

}
a[high]=a[low];
}
a[low]=temp;
return low;
}
public static void quickSort(int[] a,int low, int high){
int middle;
if (low<high) {
  middle = getMiddle(a, low, high);
  quickSort(a, low, middle-1);
  quickSort(a, middle+1, high);
}
}
}