简体   繁体   中英

How to insert random numbers into an array and sort them

My code is supposed to take the random number generated in the random method and sort them but it's only giving me one number.

My program is a random number generator that is supposed to make 1000 numbers that I can sort but my code only inserts one number into the array.

public static void main(String[] args) {
    // write

    int max = 1000;
    int min=0;
    int range = max - min + 1;

    // generate random numbers within 1 to 10
    for (int i = 0; i < 1000; i++) {
        int rand = (int) (Math.random () * range) + min;
        System.out.println ( rand );




        int array[] = {rand};
        int size = array.length;

        for ( i = 0; i < size - 1; i++) {
            int min1 = i;

            for (int j = i + 1; j < size; j++) {
                if (array[j] < array[min1]) {
                    min = j;
                }
            }
            int temp = array[min1];
            array[min1] = array[i];
            array[i] = temp;
        }

        for (int k = 0; k < size; i++) {
            System.out.print(" " + array[i]);
        }
    }
}

You need to break your program into separate steps:

  1. Insert all the random numbers into the array
  2. Sort the array
  3. Print the contents of the array

Few problems I noticed:

  1. Since you want to generate 1000 numbers from 1-10, max and min should have values of 10 and 1, respectively.
  2. array should be declared before you start inserting values. It should also have a fixed size of 1000.
  3. Your bubble sort algorithm also had some errors which led to incorrect output. If you wish to sort the array from greatest to least instead, simply change the > to < in the condition of the if statement.

I also decided to use Arrays.toString() to print the array instead of the loop.

public static void main(String[] args) {
    int max = 10;
    int min = 1;
    int range = max - min + 1;
    int size = 1000;
    int[] array = new int[size];

    for (int i = 0; i < size; i++) {
        int rand = (int) (Math.random() * range + min);
        array[i] = rand;
    }

    int temp = 0;
    for (int i = 0; i < size; i++) {
        for (int j = 1; j < size - i; j++) {
            if (array[j - 1] > array[j]) {
                temp = array[j - 1];
                array[j - 1] = array[j];
                array[j] = temp;
            }
        }
    }
    System.out.println(Arrays.toString(array));
}

your code will result an ArrayIndexOutException. below is the code change from your code,i dont change too much so you can compare them and find your mistakes,wish good:D

 public static void main(String[] args) {

    int max = 1000;
    int min=0;
    int range = max - min + 1;


    int[] array = new int[range];
    // generate random numbers within 1 to 10
    for (int i = 0; i < 1000; i++) {
        int rand = (int) (Math.random () * range) + min;
        array[i] = rand;
    }

    int size = array.length;
    for (int i = 0; i < size; i++) {
        int min1 = i;

        for (int j = i + 1; j < size; j++) {
            if (array[j] < array[min1]) {
                min1 = j;//here min1
            }
        }
        int temp = array[min1];
        array[min1] = array[i];
        array[i] = temp;
    }

    for (int k = 0; k < size; k++) {
        System.out.print(" " + array[k]);
    }
}

let me explain it more clearly,in the OP's code there has some questions,two majors: one:

for ( i = 0; i < size - 1; i++) {
        int min1 = i;

        for (int j = i + 1; j < size; j++) {
            if (array[j] < array[min1]) {
                min = j;
            }
        }
        int temp = array[min1];
        array[min1] = array[i];
        array[i] = temp;
    }

will never run,because the array size is 1,so the for loop phrase will be ignore without running(mean for(int i = 0; i < 0; i++){....}).

two:

for (int k = 0; k < size; i++) {
        System.out.print(" " + array[i]);
    }

beacause the array size is 1,so when array[1] will throw index out exception.so the outermost loop will just run once then throw a exception.

:D

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