简体   繁体   中英

Bubble Sort of a Random number in java

i cant sort my random numbers ...

the output is all zero's please help me ... this is my code

          import java.util.Random;
          import java.util.Arrays;

      class BubbleSort{
        public static void main(String [] args){
    Random g = new Random();

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

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

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

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

            }
                }
        return x;   
                }
                          }

output ..

Random Numbers: 48 12 89 18 99 89 50 56 56 70

Sorted Numbers:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Process completed.

Your random numbers are never populated. You simple print them out to the user, but what you pass to your bubble sort is an array containing 0 . Hence the output.

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

Should be

int [] number = new int [10];

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

You never assigned the values to array.

Change

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

to

  for (int d = 0 ; d<number.length ; d++){
     int RandomG = g.nextInt(100)+1;
     System.out.print("\t" + RandomG);
     number[d] = RandomG ; // See here Added this line.
   }

You generate and print your random numbers but never put them in the array.

In general you are better off printing the array after filling it rather than doing it at the same time since that would have immediately showed up the problem.

The output is all zero' is because you just generate random numbers, but not assign them to random number array (int[]) named number .

See the code of yours below, the random number is not assigned to int[] array named number

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

Then you are trying to pass the int[] random array number to method BubbleSortAsceMethod . Note, at this moment, random array contains default value -- zeros'.

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

You need to make some change like

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;//<=====Assign the random number to array
    }

In addition, there is some mistake for the for-loops in method BubbleSortAsceMethod ,

you need to make the following change to make the Bubble sort correctly.

Change

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

        }

To

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

        }
    }

Here is the code perfectly working fine : You can reverse the sign from > to < to get the descending order in the code.

 import java.util.*; public class ArrayClassSort { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Total numbers :"); int n = s.nextInt(); double a[] = new double[n]; for (int i = 0; i < a.length; i++) { a[i] = Math.random(); System.out.println("Random generated total numbers are: " + a[i]); } System.out.println("\\n Sorted Numbers are: " + Arrays.toString(BubbleSortDesc(a))); } private static double[] BubbleSortDesc(double[] a) { for (int i = 0; i < a.length - 1; i++) { for (int j = 1; j < a.length - i; j++) { if (a[j - 1] > a[j]) { double temp = a[j - 1]; a[j - 1] = a[j]; a[j] = temp; } } } return a; } } 

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