package sdu.ocean;
public class quickSort {
public static void main(String[] args) {
int a[] = { 0, 10, 4, 1, 6, 4, 8, 9, 5, 23, 12, 65, 43 };//不用0位置
new quickSort().quick(a, 1, 12);
for(int i = 1 ; i < a.length; i++){
System.out.print(a[i]+" ");
}
}
public int partition(int a[], int f, int r) {
int point = f;
int i = f + 1;
int j = r;
int p = a[point];
while(true){
while (i<13 && a[i] < p){ //判断下是否越界
i++;
}
while (j>0 && a[j] > p){ //判断下是否越界
j--;
}
if (j <= i) {
break;
} else {
int x = a[j];
a[j] = a[i];
a[i] = x;
}
}
a[point] = a[j];
a[j] = p;
return j;
}
public void quick(int a[] , int f, int r){
if(f<r){
int p = partition(a,f,r);
quick(a,f,p-1);
quick(a,p+1,r);
}
}
}
快速排序
最新推荐文章于 2024-07-17 10:35:12 发布