BubbleSort

冒泡排序

排序父类

  • 排序模板用于继承

    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);
    }
}

冒泡排序

BubbleSort.java

package MYSORT;

public class BubbleSort extends Sort {
    public static void sort(Comparable[] a, boolean printProcess)
    {
        for (int i=a.length-1;i>0;i--)
        {
            for (int j=0;j<i;j++)
            {
                if (less(a[j+1],a[j])) exchange(a, j, j+1);
            }
            if (printProcess) print(a);
        }
    }
}

排序测试

SortTest.java

package 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("冒泡排序过程:");
        BubbleSort.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
冒泡排序过程:
5| 5| 6| 7| 9| 1| 9| 12| 35| 14| 62| 17| 2| 23| 88| 45| 76| 98| 145| 126| 26| 45| 18| 13| 24| 181| Disordered
5| 5| 6| 7| 1| 9| 9| 12| 14| 35| 17| 2| 23| 62| 45| 76| 88| 98| 126| 26| 45| 18| 13| 24| 145| 181| Disordered
5| 5| 6| 1| 7| 9| 9| 12| 14| 17| 2| 23| 35| 45| 62| 76| 88| 98| 26| 45| 18| 13| 24| 126| 145| 181| Disordered
5| 5| 1| 6| 7| 9| 9| 12| 14| 2| 17| 23| 35| 45| 62| 76| 88| 26| 45| 18| 13| 24| 98| 126| 145| 181| Disordered
5| 1| 5| 6| 7| 9| 9| 12| 2| 14| 17| 23| 35| 45| 62| 76| 26| 45| 18| 13| 24| 88| 98| 126| 145| 181| Disordered
1| 5| 5| 6| 7| 9| 9| 2| 12| 14| 17| 23| 35| 45| 62| 26| 45| 18| 13| 24| 76| 88| 98| 126| 145| 181| Disordered
1| 5| 5| 6| 7| 9| 2| 9| 12| 14| 17| 23| 35| 45| 26| 45| 18| 13| 24| 62| 76| 88| 98| 126| 145| 181| Disordered
1| 5| 5| 6| 7| 2| 9| 9| 12| 14| 17| 23| 35| 26| 45| 18| 13| 24| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
1| 5| 5| 6| 2| 7| 9| 9| 12| 14| 17| 23| 26| 35| 18| 13| 24| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
1| 5| 5| 2| 6| 7| 9| 9| 12| 14| 17| 23| 26| 18| 13| 24| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
1| 5| 2| 5| 6| 7| 9| 9| 12| 14| 17| 23| 18| 13| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 14| 17| 18| 13| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 14| 17| 13| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 181| Disordered
1| 2| 5| 5| 6| 7| 9| 9| 12| 14| 13| 17| 18| 23| 24| 26| 35| 45| 45| 62| 76| 88| 98| 126| 145| 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
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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值