冒泡排序

来源:互联网 发布:php httpclient cookie 编辑:程序博客网 时间:2024/06/06 23:55
//冒泡排序;
package TestMaopao;
public class Test {
public static void main(String[] args) {
int[] score = new int[]{88, 52, 145, 25, 98, 100, 78, 65};
System.out.println("-----还未排序的原始数据-----");
for (int j = 0; j < score.length; j++) {
System.out.print(score[j] + " ");
}
System.out.println("\n-----开始冒泡排序-----");
int temp;
//外层循环控制轮数 长度-1轮 
for (int i = 0; i < score.length - 1; i++) {
//内层循环控制每轮比较次数
for (int j = 0; j < score.length - i - 1; j++) {
//当前数字与下个数字作比较 如果当前比下个大 交换
if (score[j] > score[j + 1]) {
//交换需要借助第三个变量
temp = score[j];
score[j] = score[j + 1];
score[j + 1] = temp;
}
}
}
for (int j = 0; j < score.length; j++) {
System.out.print(score[j] + " ");
}
}
}
0 0