快速排序
public void QuickSort(int[] arr,int left,int right){
}
// 划分方式一
public static int partition(int[] arr, int left, int right){
int pivot = arr[left];
int i = left;
int j = right;
while(i<j){
while(arr[j] >= pivot && i < j) j--;
while(arr[i] <= pivot && i < j) i++;
if(i < j){
swap(arr,i,j);
}
}
arr[left]=arr[i];
arr[i]=pivot;
System.out.println(arrayToString(arr,"排序"));
return i;
}
// 划分方式二
public static int partition(int[] arr, int left, int right){
int pivot = arr[left];
int lt = left;
for(int i = left + 1;i <= right;i++){
if(arr[i]<pivot){
swap(arr,lt,i);
lt++;
}
}
arr[lt] = pivot;
return lt;
}
网友评论