public class QuickSort7 {
public static void main(String[] args) {
int [] arr = { -9,78,0,23,-567,70,66 };
sort(arr, 0, arr.length-1);
System.out.println("排序后:" +Arrays.toString(arr));
}
public static void sort( int [] arr, int left , int right ) {
int l = left;
int r = right;
int p = arr[(l+r)/2];
int temp = 0;
while( l<r ) {
while( arr[l] < p ) {
l+=1;
}
while( arr[r] >p ) {
r-=1;
}
if( l >= r ) {
break;
}
temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
if( arr[l] == p ) {
r-=1;
}
if( arr[r] == p ) {
l+=1;
}
}
//防栈溢出
if( l == r ) {
l+=1;
r-=1;
}
if( left < r ) {
sort(arr, left, r);
}
if( right > l ) {
sort(arr, l, right);
}
}
}
【java】【快速排序】
最新推荐文章于 2023-06-04 10:54:50 发布