数据结构:冒泡排序(bubble sort)_数据

public class BubbleSort {
    public static void main(String[] args) {

        int[] arr = {3,9,-1,10,20};
        
        bubbleSort(arr);

        for (int i = 0; i < arr.length; i++) {
            System.out.printf(arr[i]+" ");
        }
    }

    static void bubbleSort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            boolean isChange = false;
            for (int j = 0; j < arr.length-i-1; j++) {
                if(arr[j] > arr[j+1]){
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                    isChange = true;
                }
            }

            if(!isChange){
                break;
            }
        }
    }
}