简体   繁体   中英

How can I sort an amount of randomly generated numbers defined by the user in Java?

Hey there Stack Overflow community, so I'm still new to Java but I am trying to learn how to sort. Right now my program creates n amount of random numbers from a range of 1 - 10. Although how I would go about putting these numbers into an array to be sorted, I'm not too sure on. Should i go about doing a bubble sort instead of Arrays.sort?

Here's my code

public static final void main(String aArgs){

    //User inputs a number for the amount of random numbers to generate
    String UserNumbers = JOptionPane.showInputDialog("How many numbers would you like to generate?");

    //The unknown amount of numbers "n" is converted from the "UserNumbers" String to an int
    int n = Integer.parseInt(UserNumbers);

    //Random number generator generating the amount of numbers as defined by the user
    Random randomGenerator = new Random();
    for (int idx = 1; idx <= n; ++idx){
      int randomInts = randomGenerator.nextInt(10);

    //Now to create an array for the random numbers to be put into so they can be sorted
      int ArrayToSort[] = new int[n];

      ArrayToSort[0] = randomInts;
      Arrays.sort(ArrayToSort);     
      System.out.println(ArrayToSort);
    }
}

}

I suspect you are not asking whether to use bubble sort because it's faster/slower then Arrays.sort but instead as Arrays.sort doesn't work for you.

I think this is due to the fact your not putting the random numbers you generated into the array you sort

Instead, try this code:

public static final void main(String args){

    //User inputs a number for the amount of random numbers to generate
    String userNumbers = JOptionPane.showInputDialog("How many numbers would you like to generate?");

    //The unknown amount of numbers "n" is converted from the "userNumbers" String to an int
    int n = Integer.parseInt(userNumbers);

    //Random number generator generating the amount of numbers as defined by the user
    int arrayToSort[] = new int[n];
    Random randomGenerator = new Random();
    for (int idx = 0; idx < n; ++idx){
      arrayToSort[idx] = randomGenerator.nextInt(10);
    }
    Arrays.sort(arrayToSort);     
    System.out.println(arrayToSort);
}

The problem with your code is that you are trying to populate an array of size n with random numbers, sort it and then print it, but your code generates in each iteration a random number, allocated an n sized array, put's the random number in slot 0 of the array and sort it, and print it (doint this n times) - which won't get the same effect ofcourse

BTW, Random.nextInt(10) return a random number between 0 and 9, not 1 and 10. to achieve what you want you will need to add 1 to that random value

Arrays.java 's sort method uses quicksort for arrays of primitives and merge sort for arrays of objects. I believe that most of time quicksort is faster than merge sort and costs less memory.

Source: Why does Java's Arrays.sort method use two different sorting algorithms for different types?

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