Java排序算法优化--快速排序【温故而知新】

/**
 *
 * @author Fly
 */
public class QuickSort {

    public void quicksort(int[] a, int f, int e) {
        int left = f, right = e;
        if (f < e) {
            while (f < e) {
                while (a[f] < a[e] && f < e) {
                    f++;
                }
                if (f < e) {
                    a[e] ^= a[f];
                    a[f] ^= a[e];
                    a[e] ^= a[f];
                    e--;
                }
                while (a[f] <= a[e] && f < e) {
                    e--;
                }
                if (f < e) {
                    a[e] ^= a[f];
                    a[f] ^= a[e];
                    a[e] ^= a[f];
                    f++;
                }
            }
            quicksort(a, left, f - 1);
            quicksort(a, f + 1, right);
        }
    }

    public void printArray(int[] a) {
        for (int i : a) {
            System.out.print(i + ",");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] a = {2, 3, 1, 5, 7, 8, 9, 0, 11, 10, 12, 13, 14, 4, 6};
        QuickSort quicksort = new QuickSort();
        quicksort.printArray(a);
        quicksort.quicksort(a, 0, a.length - 1);
        quicksort.printArray(a);
    }
}

你可能感兴趣的:(Java排序算法优化--快速排序【温故而知新】)