冒泡排序

package maopao;

import java.util.Arrays;

class MpSort{

/*
* 冒泡排序
* @author liuchen
* @param needNext 用于表示当出现一个b[j] > b[j+1]的情况,就马上停止跳出循环。
*
* 通过b.length -1 次循环,每次遍历都将最大的放在后面。每次都从头开始循环 ,比较还剩的个数。
*/

public static void mpSort(int[] b){
boolean needNext = true;
int temp = 0;
for (int i = 1; i < b.length; i++) {
needNext = false;
for (int j = 0; j < b.length - i; j++) {
if(b[j] > b[j+1]){
temp = b[j+1];
b[j+1] = b[j];
b[j] = temp;
}
else needNext = true;
}
}
}

public static void main(String[] args){
int[] b = {4,5,34,5,76,45,34,7657,34};
System.out.println(Arrays.toString(b)); //数组输出的格式转变
mpSort(b);
System.out.println(Arrays.toString(b));

}
}

posted @ 2013-05-14 22:48  弦月ll  阅读(105)  评论(0)    收藏  举报