经典算法体会之冒泡排序

通过java语法理解冒泡排序,原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换,这样一趟过去后,最大或最小的数字被交换到了最后一位,然后再从头开始进行两两比较交换,直到倒数第二位时结束。最核心的就是两两比较,将最小的升上去。

package test;


public class aa{
    public static void main(String[] args){
         int score[] = {67, 69, 75, 87, 89, 90, 99, 100};//数组内有8个无序元素
        for (int i = 0; i < score.length -1; i++){    //最多做n-1趟排序
             for(int j = 0 ;j < score.length - i - 1; j++){    //对当前无序区间score[0......length-i-1]进行排序(
                 if(score[j] < score[j + 1]){    //把小的值交换到后面
                     int temp = score[j];
                     score[j] = score[j + 1];
                     score[j + 1] = temp;
                 }
             }            
             System.out.print("第" + (i + 1) + "次排序结果:");
             for(int a = 0; a < score.length; a++){
                 System.out.print(score[a] + "\t");
             }
             System.out.println("");
         }
             System.out.print("最终排序结果:");
             for(int a = 0; a < score.length; a++){
                System.out.print(score[a] + "\t");//输出最终排序结果
       }
     }
 }




你可能感兴趣的:(JAVA,算法C)