import java.util.Arrays;//导入支持
public class QuickSort {
public static void sort(int a[], int low,int hight) {
int i, j, index;
if (low > hight) {
return;
}
i = low;
j = hight;
index = a[i]; // 用数组的第一个记录做标志
while (i < j) { // 从数组的两端交替向中间走
while (i < j && a[j] >= index)
j--;
if (i < j)
a[i] = a[j];
while (i < j && a[i] < index)
i++;
if (i < j)
a[j] = a[i];
}
a[i] = index;
sort(a, low, i - 1); // 对低子数组进行递归排序
sort(a, i + 1, hight); // 对高子数组进行递归排序
}
public static void quicksort(int a[]) {
sort(a, 0, a.length - 1);
}
public static void main(String[] args) {
int a[] = { 78,237,34,90,13,27,768,34,5,6,732,49 };
quicksort(a);
System.out.println(Arrays.toString(a));
}
}
---------------------------或者------------------------------
import java.util.Arrays;
public class QuickSort2{
public static void sort(int a[],int low,int height){
int i,j,index;
if (low>height){
return;
}
i = low;
j = height;
index = a[i];
while(i!=j){
while(i<j&&a[j]>=index){
j--;
}
while(i<j&&a[i]<=index){
i++;
}
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
a[low]=a[i];
a[i]=index;
sort(a,low,i-1);
sort(a,i+1,height);
}
public static void quicksort(int a[]){
sort(a,0,a.length-1);
}
public static void main(String args[]){
int a[] = { 78,237,34,90,13,27,768,34,5,6,732,49 };
quicksort(a);
System.out.print("end");
System.out.println(Arrays.toString(a));
}
}