1. 程式人生 > >java實現快排

java實現快排

public class Quicksort {
    public void exchange(int [] array, int i, int j)
    {
        int temp;
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public int partitin(int [] array, int p, int r)
    {
        int x = array[r-1];
        int i = p-1;
        for
(int j=p;j<=r-2;j++) { if(array[j]<=x) { i = i + 1; exchange(array,i,j); } } exchange(array,i+1,r-1); return i+1; } void quicksort(int [] array, int p, int r) { if(p<r) { int
q = partitin(array,p,r); quicksort(array,p,q-1); quicksort(array,q+1,r-1); } } public static void main(String [] args) { int [] array = {9,7,2,4,5,3,6,1,0}; Quicksort qs = new Quicksort(); qs.quicksort(array,0,array.length); for
(int i=0;i<array.length;i++) { System.out.print(array[i]+" "); } } }