QuickSort

快速排序(Java实现)

排序父类

  • 排序模板用于继承

    Sort.java

package MYSORT;

public class Sort {
    public static void sort(Comparable[] a, boolean printProcess)
    {}

    public static boolean less(Comparable v, Comparable w)
    { return v.compareTo(w) < 0; }

    public static void exchange(Comparable[] a, int i, int j)
    {
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    public static void print(Comparable[] a)
    {
        String status = "Disordered";
        if (isSorted(a)) status="Ordered";
        for (int i = 0; i<a.length; i++)
            System.out.print(a[i]+"| ");
        System.out.print(status);
        System.out.println();
    }

    public static boolean isSorted(Comparable[] a)
    {
        for (int i = 0; i<a.length-1; i++)
            if (less(a[i+1], a[i]))
                return false;
            return true;
    }
}

元素类型

  • 实现了Comparable接口

  • 用作排序的元素

    intForSort.java

package MYSORT;

public class intForSort implements Comparable<intForSort> {
    private int data;
    public intForSort(int data)
    {this.data = data;}
    @Override
    public int compareTo(intForSort o) {
        if (this.data>o.data) return 1;
        if (this.data==o.data) return 0;
        return -1;
    }

    @Override
    public String toString() {
        return String.valueOf(data);
    }
}

快速排序

QuickSort.java

package MYSORT;

public class QuickSort extends Sort
{
    public static void sort(Comparable[] a, boolean printProcess)
    {
        sort(a, 0,a.length-1, printProcess);
    }
    private static void sort(Comparable[] a, int lo, int hi, boolean printProcess)
    {
        if (hi<=lo) return;
        int j = partitiion(a, lo, hi);
        sort(a, lo, j-1, printProcess);
        sort(a, j+1, hi, printProcess);
        print(a);
    }
    private static int partitiion(Comparable[] a, int lo, int hi)
    {
        int i=lo,j=hi+1;
        Comparable v = a[lo];
        while (true)
        {
            while (less(a[++i],v)) if (i==hi) break;
            while (less(v,a[--j])) if (j==lo) break;
            if (i>=j) break;
            exchange(a,i,j);
        }
        exchange(a,lo,j);
        return j;
    }
}

排序测试

SortTest.java

ppackage MYSORT;

public class SortTest {
    public static void main(String[] args) {
        int[] data = {5,5,9,6,7,9,1,12,35,62,14,181,17,2,23,88,45,76,98,145,126,26,45,18,13,24};
        intForSort []elems = new intForSort[data.length];
        for (int i = 0;i<data.length;i++)
        {
            elems[i] = new intForSort(data[i]);
        }
        System.out.println("排序前:");
        Sort.print(elems);
        System.out.println("快速排序过程:");
        QuickSort.sort(elems,true);
        System.out.println("排序后:");
        Sort.print(elems);
    }
}

运行结果

排序前:
5| 5| 9| 6| 7| 9| 1| 12| 35| 62| 14| 181| 17| 2| 23| 88| 45| 76| 98| 145| 126| 26| 45| 18| 13| 24| Disordered
快速排序过程:
1| 2| 5| 6| 7| 9| 9| 12| 35| 62| 14| 181| 17| 5| 23| 88| 45| 76| 98| 145| 126| 26| 45| 18| 13| 24| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 45| 35| 45| 62| 126| 145| 98| 76| 88| 181| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 45| 35| 45| 62| 126| 145| 98| 76| 88| 181| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 45| 35| 45| 62| 126| 145| 98| 76| 88| 181| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 45| 35| 45| 62| 126| 145| 98| 76| 88| 181| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 45| 35| 45| 62| 126| 145| 98| 76| 88| 181| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 126| 145| 98| 76| 88| 181| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 126| 145| 98| 76| 88| 181| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 126| 145| 98| 76| 88| 181| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Ordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Ordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Ordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Ordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Ordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Ordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Ordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Ordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Ordered
排序后:
1| 2| 5| 5| 6| 7| 9| 9| 12| 13| 14| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Ordered

参考资料

Robert Sedgewick and Kevin Wayne《Algorithms》FOURTH EDITION

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值