一个快速排序程序

无意中看到一个游戏中有着快速排序的源代码,备份下

import java.util.Comparator;

public class QuickSorter<Type> extends Sorter<Type> {
    public void sort(Type[] array, int count, Comparator<Type> comparator) {
        quicksort(array, 0, count - 1, comparator);
    }
    
     
        // quicksort a[left] to a[right]
    public void quicksort(Type[] a, int left, int right, Comparator<Type> comparator) {
        if (right <= left) return;
        int i = partition(a, left, right, comparator);
        quicksort(a, left, i - 1, comparator);
        quicksort(a, i + 1, right, comparator);
    }
       
    // partition a[left] to a[right], assumes left < right
    private int partition(Type[] a, int left, int right, Comparator<Type> comparator) {
        int i = left - 1;
        int j = right;
        while (true) {
            while (comparator.compare(a[++i], a[right]) < 0) {     // find item on left to swap
            }                              // a[right] acts as sentinel
            while (comparator.compare(a[right], a[--j]) < 0) {    // find item on right to swap
                if (j == left) { 
                    break;                 // don't go out-of-bounds
                }
            }
            if (i >= j) {
                break;                     // check if pointers cross
            }
            Type swap = a[i];                 // swap two elements into place
            a[i] = a[j];
            a[j] = swap;
        }
        Type swap = a[i]; // swap with partition element
        a[i] = a[right];
        a[right] = swap;
        return i;
    }
}

你可能感兴趣的:(sort)