Java 数组和冒泡排序

更新!!


我设法使程序生成 50 个随机整数(来自 10,000 个随机整数数组)。但是,我正在努力使冒泡排序方法对完整值(即 4579 和 3457)进行排序,而不仅仅是对个位数(3、4、4、5、5、7、7、9)进行排序


这是我正在使用的代码:


public class RandomNumbers

{

   public static void main(String[] args)

   {

      int[] randomIntArray = new int[10000];


      for(int i = 0; i<randomIntArray.length; i++)

         randomIntArray[i] = (int)(Math.random() * 10000);


      for(int i = 0; i < 50; i++)

         System.out.println(randomIntArray[i]);


      System.out.println("Original order: ");

      for(int i = 0; i < 50; i++)

         System.out.print(randomIntArray[i] + "  ");


      IntBubbleSorter.bubbleSort(randomIntArray);


      System.out.println("\nSorted order: ");

      for(int i = 0; i < 50; i++)

         System.out.print(randomIntArray[i] + " ");


      System.out.println();

   }


}


public class IntBubbleSorter {

   public static void bubbleSort (int[] randomIntArray) {

      int lastPost;

      int index;

      int temp;


      for(lastPost = randomIntArray.length - 1; lastPost >= 0; lastPost--)

      {

         for(index = 0; index <= lastPost - 1; index++)

         {

            if(randomIntArray[index] > randomIntArray[index + 1])

            {

               temp = randomIntArray[index];

               randomIntArray[index] = randomIntArray[index + 1];

               randomIntArray[index + 1] = temp;

            }

         }

      }

   }

}

我当前的输出看起来像这样(为了便于阅读,缩短为 5 个整数):


Original order: 3898  6015  462  1960  8040

Sorted order: 0 1 2 2 3


翻过高山走不出你
浏览 178回答 2
2回答

慕神8447489

首先在这个循环的主函数中:&nbsp;for(int element = 0; element < 50; element++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;values[element] = randomNumbers.nextInt(10000);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }您在 10,000 数组中仅创建 50 个随机数,而数组中的其他数字将默认分配为 0。第二:尝试 instede 这行:IntBubbleSorter.bubbleSort(values); 这行:bubbleSort(values);

偶然的你

如果您的 main 和 bubbleSort 函数位于不同的类中,请确保它们位于同一个包(文件夹)中。randomNumbers.nextInt(10000)意味着下一个随机数应该在 0 到 10000 之间,并且您只生成 50 个随机数。我创建了两个类,一个用于主函数,另一个用于冒泡排序。您应该将它们更改为对您有益的任何内容,但请确保它们位于同一文件夹中(同一包)主类:import java.util.Random;public class MainClass {&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Initialize Array&nbsp; &nbsp; &nbsp; &nbsp; int [] values = new int[10000];&nbsp; &nbsp; &nbsp; &nbsp; Random randomNumbers = new Random();&nbsp; &nbsp; &nbsp; &nbsp; for(int index = 0; index < values.length; index++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; values[index] = randomNumbers.nextInt(10000);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Original order: ");&nbsp; &nbsp; &nbsp; &nbsp; for(int index = 0; index < 50; index++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(values[index] + "&nbsp; ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; IntBubbleSorter.bubbleSort(values);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\nSorted order: ");&nbsp; &nbsp; &nbsp; &nbsp; for(int index = 0; index < 50; index++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print(values[index] + "&nbsp; ");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; }}IntBubbleSorter 类:public class IntBubbleSorter {&nbsp; &nbsp; public static void bubbleSort (int[] array) {&nbsp; &nbsp; &nbsp; &nbsp; int lastPost;&nbsp; &nbsp; &nbsp; &nbsp; int index;&nbsp; &nbsp; &nbsp; &nbsp; int temp;&nbsp; &nbsp; &nbsp; &nbsp; for(lastPost = array.length - 1; lastPost >= 0; lastPost--)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(index = 0; index <= lastPost - 1; index++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(array[index] > array[index + 1])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = array[index];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[index] = array[index + 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[index + 1] = temp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java