冒泡排序

来源:互联网 发布:淘宝金牌店铺联盟 编辑:程序博客网 时间:2024/06/05 08:47

public class 冒泡 {

public static void main(String[] args) {    int score[] = {6,3,8,2,9,1};    BubbleSort(score);}public static void BubbleSort(int[] score){    System.out.println("冒泡前:");    for (int i = 0; i < score.length; i++) {        System.out.print(score[i] + "\t");    }    System.out.println();    int len = score.length;    for (int i = len; i > 1; i--) {//3个数进行排序,只需要进行2趟就可以        for (int j = 0; j < i - 1; j++) {            if(score[j] > score[j+1]){                int temp = score[j];                score[j] = score[j+1];                score[j+1] = temp;            }        }    }    System.out.println("冒泡后:");    for (int i = 0; i < score.length; i++) {        System.out.print(score[i] + "\t");    }}

}

原创粉丝点击