冒泡排序

/**
 * 冒泡排序
 * @author TMAC-J
 *
 */
public class BubbleSort {
    
    private int[] array;
    
    public BubbleSort(int[] array) {
        this.array = array;
    }
    /**
     * 按从小到大的顺序排列
     */
    public void calculate(){
        for(int i = array.length-1;i>0;i--){
            for(int j = 0;j<i;j++){
                if(array[j]>array[j+1]){
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                }
            }
        }
    }
    
    public static void main(String[] args) {
        int[] a = {10,9,8,7,6,5,4,3,2,1};
        BubbleSort bubbleSort = new BubbleSort(a);
        bubbleSort.calculate();
        for(int i = 0;i<a.length;i++){
            System.out.println(a[i]);
        }
    }
    
}

输出结果:

1
2
3
4
5
6
7
8
9
10

原文地址:https://www.cnblogs.com/yzjT-mac/p/6253575.html