您的位置:首页 > 其它

冒泡算法

2015-08-05 19:27 369 查看

冒泡排序

/**
* 冒泡算法
* 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]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  排序 算法