有关java的冒泡排序
第一次写博客,不太熟悉请见谅。
冒泡排序:按名字来 如气泡一下 小的在下面 大的在后面;
在java语言中通过两个for循环来遍历,外层循环从数组的第二个数的开始,内层循环从数组的第一个数开始遍历,第一个数和第二个数比较大小,大于时两个值进行交换,重点在于将大的数往后放,内层循环每次都要循环数组的长度的次数,将最大的数与外层的数对比一下,大于就换,排序的次数由数组长度决定。
public class ArrayDemo07 {
public static void main(String[] args) {
int []score = {45,54,91,75,85,97,55,31,12};
for(int i=1;i<score.length;i++) {
for(int j=0;j<score.length;j++) {
if(score[i]<score[j]) {
int temp=score[i];
score[i]=score[j];
score[j]=temp;
}
}System.out.print("第"+i+"次排序的结束\t");
for(int n=0;n<score.length;n++) {
System.out.print(score[n]+"\t");
}
System.out.println("");
}
System.out.print("最终排序的结束\t");
for(int n=0;n<score.length;n++) {
System.out.print(score[n]+"\t");
}
}
}
有任何错误请直接在评论区指出,带我进步!!!