简体   繁体   中英

How to sort random number in bubble sort

I'm trying to put run a program that would print 1000 random number and sorted them in bubble sorting but when I run this code, it'll print my 1000 random numbers but wont sort it out. Please help me understand.

import java.util.Random;
import java.util.Arrays;
public class JavaApplication3 {
    public static void main(String [] args){
        Random g = new Random();

        int [] number = new int [1000];

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

        System.out.print("\nSorted Numbers:"+Arrays.toString(BubbleSortAsceMethod(number)));
    }
    public static int [] BubbleSortAsceMethod(int[] number){
        int temp;

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

Take time, pen and paper and draw what your code actually does. Although the indentation is hard to read, I believe your mistake lies in the double for loop (J is wrong) good luck!

The numbers are out of order and need to be swapped when number[j-1] is greater than number[j] (not less than), unless you want them sorted in reverse order.

The inner loop should be j < number.length - i, not j < number.length - 1

       for (int j = 1 ; j < number.length - i ; j++){
            if (number[j-1] > number[j]){

You didn't initialize your "number" array with your random numbers instead you just set its size to 1000. I think that's why it didn't sorted out. you have to put your "number" array in the first for loop and initialize/populate it with random values like this:-

for (int d = 0 ; d<number.length ; d++){
    int RandomG = g.nextInt(1000)+1;
    number[d] = RandomG;
    System.out.print(" " +RandomG);
    }

Here is the full code:-

package stackoverflow;
import java.util.Random;
import java.util.Arrays;
/**
 *
 * @author User
 */
public class StackOverFlow {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Random g = new Random();

        int [] number = new int [1000];

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

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

    }
    public static int [] BubbleSortAsceMethod(int[] number){
        int temp;

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

}

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