/*
2011-9-18
author:BearFly1990
*/
package algorithm;
import java.util.Arrays;
public class BFQuickSort {
public static void main(String[] args) {
int[] a = new int[] {5, 9, 8, 4, 7, 3, 6, 2};
System.out.println(Arrays.toString(a));
sort(a, 0, a.length - 1);
System.out.println(Arrays.toString(a));
}
private static void sort(int[] a, int low, int high) {
int pivot;
if(low < high){
pivot = partition(a,low,high);
sort(a,low,pivot-1);
sort(a,pivot+1,high);
}
}
private static int partition(int[] a, int low, int high) {
int pivotkey;
pivotkey = a[low];
while(low < high){
while(low < high && a[high] >= pivotkey){
high--;
}
swap(a,low,high);
while(low < high && a[low] <= pivotkey){
low++;
}
swap(a,low,high);
}
return low;
}
private static void swap(int[] a, int left, int right) {
int temp = a[left];
a[left] = a[right];
a[right] = temp;
}
}
快速排序(更好理解)
最新推荐文章于 2024-03-04 17:29:58 发布