首页 > 代码库 > 泛型实现快速排序

泛型实现快速排序

package com.rainfool.quicksort;public class Test2 {    public static void main(String[] args) {        Integer[] a = { 2, 8, 7, 1, 3, 5, 6, 4 };        quickSort(0, a.length - 1, a);        for (int i = 0; i < a.length; i++) {            System.out.print(a[i] + " ");        }    }    public static <T extends Comparable<? super T>> void quickSort(int p,            int r, T[] A) {        if (p < r) {            int q = partition(p, r, A);            quickSort(p, q - 1, A);            quickSort(q + 1, r, A);        }    }    public static <T extends Comparable<? super T>> int partition(int p, int r,            T[] A) {        int i = p - 1;        for (int j = p; j < r; j++) {            if (A[j].compareTo(A[r]) < 0) {                i++;                T t = A[j];                A[j] = A[i];                A[i] = t;            }        }        T t = A[i + 1];        A[i + 1] = A[r];        A[r] = t;        return i + 1;    }}

 

泛型实现快速排序