冒泡算法

冒泡排序

/**
 * 冒泡算法
 * Created by xueping.you on 15-8-5.
 */
public class BubbleSort {
    private final static Logger logger = LoggerFactory.getLogger(BubbleSort.class);

    public static void bubbleSort(int [] unsortArray){

        for(int i=0; i<unsortArray.length; i++){
            for(int j=0; j<unsortArray.length-i-1; j++){
                int temp = unsortArray[j];
                if(unsortArray[j]>unsortArray[j+1]){
                    unsortArray[j] = unsortArray[j+1];
                    unsortArray[j+1] = temp;
                }
            }
        }
    }

    public static void main(String []args){
        int [] array = new int[]{12,10,2,45,31,56,1,9};
        logger.info("before:{}" , array);
        bubbleSort(array);
        logger.info("after:{}", array);
    }
}

result:
19:26:20.928 [main] INFO com.qyou.data.arithmetic.BubbleSort - before:[12, 10, 2, 45, 31, 56, 1, 9]
19:26:20.938 [main] INFO com.qyou.data.arithmetic.BubbleSort - after:[1, 2, 9, 10, 12, 31, 45, 56]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章