1. 程式人生 > >java 學習(一)氣泡排序

java 學習(一)氣泡排序

public class BubbleSort{

    public static void main(String[] args){
        int a[] = {1, 23, 45, 6, 0, 99, 100, 89, 34, 56};

        System.out.println("排序前:");
        for (int i = 0; i < a.length; ++ i)
            System.out.print(a[i] + " ");

        bubbleSort(a);

        System.out.println();
        System.out
.println("排序後:"); for (int i = 0; i < a.length; ++ i) System.out.print(a[i] + " "); } public static void bubbleSort(int a[]){ int temp = 0; for (int i = 0; i < a.length - 1; ++ i){ for (int j = i; j < a.length; ++ j){ if
(a[i] > a[j]){ temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } }