排序算法:冒泡排序實現從大到小輸出成績數組

需求:逐個輸入10個學生成績,並將從大到小輸出。

分析:可使用冒泡排序將成績數組進行交換排序

冒泡排序示例代碼:

void bubbleSort(int[] a){
    int temp;
    for(int i = 1;i < a.length;i++) {
        for(int j = 0;j < a.length - i; j++) {
            if(a[j] > a[j+1]) {
                temp = a[j];
                a[j] = a[j+1];
                a[j+1] = temp;
            }
        }
        System.out.print(String.format("第 %d 步排序結果:",i));
        for(int k = 0; k < a.length; k++){
            System.out.print(a[k] + "  ");
        }
        System.out.println();
    }
}

實現從大到小打印成績數組: 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StuduentScore2 {
    // 逐個輸入10個學生的成績,並將其從大到小輸出
    // 10個學生保存到常量final int N = 10;
    // 輸入:保存到double[] scores中
    // 從大到小用冒泡排序double[] sort(double[] nums){}方法

    private static final int N = 10;
    private static double[] scores = new double[N];

    public static double[] sort(double[] nums){
        for(int k = 1; k < N; k ++) {
            for(int index = 0;index < N -k; index++){
                if(nums[index] < nums[index + 1]){
                    double tmp = nums[index];
                    nums[index] = nums[index + 1];
                    nums[index + 1] = tmp;
                }
            }
            System.out.print(String.format("第 %d 步排序結果是:",k));

            for(int i = 0; i < N; i++) {
                System.out.print(nums[i] + " ");
            }
            System.out.println();
        }
        return nums;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        int count = 0;
        while (count < N){
            while (true){
                System.out.println(String.format("請輸入第%d個學生的成績",count+1));
                String str = buf.readLine();
                scores[count] = Double.parseDouble(str);
                if(scores[count] < 0 || scores[count] >100) {
                    System.out.println("請重新輸入!");
                }else{
                    count ++;
                    break;
                }
            }
        }
        double[] scoresNew = sort(scores);
        System.out.print(String.format("這 %d位同學的成績排序爲:",N));
        for(int i = 0; i < N; i++){
            System.out.print(scoresNew[i] + "  ");
        }
    }
}

代碼實現結果如下:

請輸入第1個學生的成績
86
請輸入第2個學生的成績
72
請輸入第3個學生的成績
68
請輸入第4個學生的成績
74
請輸入第5個學生的成績
58
請輸入第6個學生的成績
92
請輸入第7個學生的成績
81
請輸入第8個學生的成績
63
請輸入第9個學生的成績
52
請輸入第10個學生的成績
80
第 1 步排序結果是:86.0 72.0 74.0 68.0 92.0 81.0 63.0 58.0 80.0 52.0 
第 2 步排序結果是:86.0 74.0 72.0 92.0 81.0 68.0 63.0 80.0 58.0 52.0 
第 3 步排序結果是:86.0 74.0 92.0 81.0 72.0 68.0 80.0 63.0 58.0 52.0 
第 4 步排序結果是:86.0 92.0 81.0 74.0 72.0 80.0 68.0 63.0 58.0 52.0 
第 5 步排序結果是:92.0 86.0 81.0 74.0 80.0 72.0 68.0 63.0 58.0 52.0 
第 6 步排序結果是:92.0 86.0 81.0 80.0 74.0 72.0 68.0 63.0 58.0 52.0 
第 7 步排序結果是:92.0 86.0 81.0 80.0 74.0 72.0 68.0 63.0 58.0 52.0 
第 8 步排序結果是:92.0 86.0 81.0 80.0 74.0 72.0 68.0 63.0 58.0 52.0 
第 9 步排序結果是:92.0 86.0 81.0 80.0 74.0 72.0 68.0 63.0 58.0 52.0 
這 10位同學的成績排序爲:92.0  86.0  81.0  80.0  74.0  72.0  68.0  63.0  58.0  52.0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章