排序篇:bubble

public static void bubbleSort() {
    int[] arr = {0, 5, 6, 333, 5, 8, 999, 7, 7, 5, 45, 3};
    for (int i = arr.length - 1; i > 0; i--) {
        for (int j = arr.length - 1; j > arr.length - i - 1; j--) {
            if (arr[j] < arr[j - 1]) {
                swap(arr, j, j - 1);
            }
        }
    }
    System.out.println(Arrays.toString(arr));
}

public static void swap(int arr[], int j, int i) {
    int tmp = arr[j];
    arr[j] = arr[i];
    arr[i] = tmp;
}

 

你可能感兴趣的:(java)