简体   繁体   English

在Java中对随机数进行冒泡排序

[英]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 随机数:48 12 89 18 99 89 50 56 56 70

Sorted Numbers:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Process completed. 排序数字:[0,0,0,0,0,0,0,0,0,0]处理完成。

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 . 您可以简单地将它们打印出给用户,但是传递给冒泡排序的是一个包含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 . 输出全为零”是因为您只是生成随机数,而没有将它们分配给名为number随机数数组(int [])。

See the code of yours below, the random number is not assigned to int[] array named number 请参见下面的代码,该随机数未分配给名为number int []数组

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 . 然后,您尝试将int []随机数组number传递给方法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 , 此外,方法BubbleSortAsceMethod中的for循环还有一些错误,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM