简体   繁体   中英

Java bubblesort 1-100

So I have most of the code completed. I just can't figure out why the sorted list is not in order from smallest to largest. The assignment was to generate 10 random integers from 1-10. Display the unsorted list. Display sorted (smallest to largest). Display arrays contents.

        int [] number = new int [10];

        System.out.print("Random Numbers:");
        for (int d = 0 ; d<number.length ; d++){
            int RandomG = g.nextInt(100) + 1;
            System.out.print("\t" + RandomG);
            number[d] = RandomG ;
        }
        System.out.print("\nSorted Numbers:"+Arrays.toString(BubbleSortAsceMethod(number)));
    }

    public static int [] BubbleSortAsceMethod(int[] x) {
        int temp;

        for (int i = 0; i < x.length; i++) {
            for (int j = 1; j < x.length -i; j++) {
                if (x[j - 1] < x[j]) {
                    temp = x[j - 1];
                    x[j - 1] = x[j];
                    x[j] = temp;
                }
            }
        }
        return x;   
    }
}

You need to change the condition

current code

 if (x[j - 1] < x[j])

Fixed code

if (x[j - 1] > x[j])

However you can enhance your code by adding isSorted flag.

public static int[] BubbleSortAsceMethod(int[] x) {
    for (int i = 1; i < x.length; i++) {
        boolean isSorted = true;

        for (int j = 0; j < x.length - i; j++) {
            if (x[j] > x[j + 1]) {
                int temp = x[j];
                x[j] = x[j + 1];
                x[j + 1] = temp;
                isSorted = false;
            }
        }
        if (isSorted) {
            break;
        }
    }
    return x;
}

I just can't figure out why the sorted list is not in order from smallest to largest.

Your problem is at x[j - 1] < x[j] . The condition is wrong where the temp variable was assigned with the small value from one-step before the current traversal value ( temp = x[j - 1] ), then it swapped with the larger value on the current traversal value which means the value before the current traversal will always have a larger value than its current traversal value. And this is why the number was sorted from the largest to lowest.

To correct it, just simply change the condition from x[j - 1] < x[j] into x[j - 1] > x[j] , where the temp value will be assigned with the largest value from one-step before the current traversal value and swapped its position with the lowest value on current traversal value. So that the value before the current traversal will always have a lower value than its current traversal value.

public static void main(String[] args) {
    int[] number = new int[10];
    Random g = new Random();

    System.out.print("Random Numbers:");
    for (int d = 0; d < number.length; d++) {
        int RandomG = g.nextInt(100) + 1;
        System.out.print("\t" + RandomG);
        number[d] = RandomG;
    }

    System.out.print("\nSorted Numbers:" + Arrays.toString(BubbleSortAsceMethod(number)));
}

public static int[] BubbleSortAsceMethod(int[] x) {
    int temp;

    for (int i = 0; i < x.length; i++) {
        for (int j = 1; j < x.length - i; j++) {
            if (x[j - 1] > x[j]) {
                temp = x[j - 1];
                x[j - 1] = x[j];
                x[j] = temp;
            }
        }
    }

    return x;
}

Note: Your code is not complete, so I need to complete it (for you) and perhaps it might be not the same with your actual code. Please give complete code next time when asking a question for others to understand it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM