1

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);
    }
}

}

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
  • No, Bubble Sort is not an efficient sort – Clint Mar 27 '15 at 19:56
  • Arrays.sort is probably the most efficient sort method. All the other sorts will be less efficient and will require custom implementation. Why are you against using `.sort()`? – Code Whisperer Mar 27 '15 at 19:58
  • Do not start variable names with capital letters, it confuses them with class/objects. `ArrayToSort[0] = randomInts;` only populates the first element of the array. – Code Whisperer Mar 27 '15 at 20:00

2 Answers2

3

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

Boaz
  • 4,549
  • 2
  • 27
  • 40
0

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?

Community
  • 1
  • 1
Touchstone
  • 5,575
  • 7
  • 41
  • 48