doocs/leetcode

Solved

Erica-Iris opened this issue · 3 comments

HERE
It should be like this

    private static void bubbleSort(int[] nums) {
        boolean hasChange = **false**;
        for (int i = 0, n = nums.length; i < n - 1 && hasChange; ++i) {
            hasChange = false;
            for (int j = 0; j < n - i - 1; ++j) {
                if (nums[j] > nums[j + 1]) {
                    swap(nums, j, j + 1);
                    hasChange = true;
                }
            }
        }
    }

Hi @Erica-Iris

if hasChange is false, it will never go into the outer for loop.

Hi @Erica-Iris

if hasChange is false, it will never go into the outer for loop.

I'm sorry that I was too anxious and didn't look carefully, which caused trouble to everyone.

HERE
It should be like this

    private static void bubbleSort(int[] nums) {
        boolean hasChange = **false**;
        for (int i = 0, n = nums.length; i < n - 1 && hasChange; ++i) {
            hasChange = false;
            for (int j = 0; j < n - i - 1; ++j) {
                if (nums[j] > nums[j + 1]) {
                    swap(nums, j, j + 1);
                    hasChange = true;
                }
            }
        }
    }