冒泡排序

此排序包括升序和降序

请参考排序接口与抽象类(java)

  • 普通冒泡排序
package com.bsc.algorithm.sort.bubble;

import com.bsc.algorithm.sort.inf.AbstractSort;

/**
 * 冒泡排序
 * 
 * @author bsc
 *
 */
public class BubbleSort<T extends Comparable<T>> extends AbstractSort<T> {
    protected void sort(T[] data, int cr) {
        int length = data.length;
        for (int i = 0; i < length - 1; i++) {
            //一次冒泡过程,把最大值移到第length - i - 1位
            for (int j = 1; j < length - i; j++) {
                if (compare(data[j - 1], data[j]) == cr) {
                    swap(data, j - 1, j);
                }
            }
        }
    }
}
  • 改良冒泡排序
package com.bsc.algorithm.sort.bubble;

import com.bsc.algorithm.sort.inf.AbstractSort;

/**
 * 冒泡排序
 * 
 * @author bsc
 *
 */
public class BubbleSort<T extends Comparable<T>> extends AbstractSort<T> {
    protected void sort(T[] data, int cr) {
        int length = data.length;
        boolean isSwap;
        do {
            isSwap = false;
            //一次冒泡过程,如果没有发生交换,证明已经排好序
            for (int j = 1; j < length; j++) {
                if (compare(data[j - 1], data[j]) == cr) {
                    swap(data, j - 1, j);
                    //有交换过
                    isSwap = true;
                }
            }
            length--;
            //直到没有交换,排序结束
        } while (isSwap);
    }
}
  • 最优冒泡排序
package com.bsc.algorithm.sort.bubble;

import com.bsc.algorithm.sort.inf.AbstractSort;

/**
 * 冒泡排序
 * 
 * @author bsc
 *
 */
public class BubbleSort<T extends Comparable<T>> extends AbstractSort<T> {

    protected void sort(T[] data, int cr) {
        int length = data.length;
        int index;
        do {
            index = 0;
            //一次冒泡过程,index记录的是最大的交换位置,后面的数已经排好序且比前面的数都大
            for (int j = 1; j < length; j++) {
                if (compare(data[j - 1], data[j]) == cr) {
                    swap(data, j - 1, j);
                    //记录下交换的位置,至少是1
                    index = j;
                }
            }
            //如果index=0,证明没有发生过交换,排序结束
            //如果index>0,证明发生过交换,记录的是最大交换位置
            length = index;
        } while (index > 0);
    }
}

测试

ArrayGenerator请参考数组数据生成器

package com.bsc.algorithm.sort.test;

import java.util.Arrays;

import com.bsc.algorithm.data.generator.ArrayGenerator;
import com.bsc.algorithm.sort.bubble.BubbleSort;
import com.bsc.algorithm.sort.inf.ISort;

public class SortTest {

    public static void main(String[] args) {
        ISort<Integer> sortInt = new BubbleSort<Integer>();

        Integer[] dataInt = ArrayGenerator.random(Integer[].class, 10, 0, 99);
        System.out.println("原序:" + Arrays.toString(dataInt));
        sortInt.sortAsc(dataInt);
        System.out.println("升序:" + Arrays.toString(dataInt) + "
");

        dataInt = ArrayGenerator.random(Integer[].class, 10, 0, 99);
        System.out.println("原序:" + Arrays.toString(dataInt));
        sortInt.sortDesc(dataInt);
        System.out.println("降序:" + Arrays.toString(dataInt) + "
");

        ISort<Character> sortChar = new BubbleSort<Character>();

        Character[] dataChar = ArrayGenerator.random(Character[].class, 10, 65, 90);
        System.out.println("原序:" + Arrays.toString(dataChar));
        sortChar.sortAsc(dataChar);
        System.out.println("升序:" + Arrays.toString(dataChar) + "
");

        dataChar = ArrayGenerator.random(Character[].class, 10, 65, 90);
        System.out.println("原序:" + Arrays.toString(dataChar));
        sortChar.sortDesc(dataChar);
        System.out.println("降序:" + Arrays.toString(dataChar) + "
");
    }
}
原文地址:https://www.cnblogs.com/bsc2012/p/9217085.html