简体   繁体   English

生成随机数,将其添加到数组中,然后使用冒泡排序对它们进行排序

[英]Generating random numbers, adding them to an array, and then sorting them using bubble sort

I am attempting to randomly generate 10 numbers between 1 and 100, store them in an array, and then sort them using the bubble sort method included below. 我试图随机生成10个介于1和100之间的数字,将它们存储在数组中,然后使用下面包含的冒泡排序方法对它们进行排序。 I am confident in the random number generation and storage, as well as the bubble sorting code. 我对随机数的生成和存储以及气泡排序代码很有信心。 I am simply having an issue calling the bubbleSort method and printing the output. 我只是在调用bubbleSort方法并打印输出时遇到问题。 Please advise. 请指教。

package chpt7_project;

import java.util.Arrays;



public class Chpt7_Project {

public static void main(String[] args) {
    // TODO Auto-generated method stub
        //Initialize array containing 10 numbers
        int[] list = new int[10];       

        //Generate 10 random numbers in the range of 1 - 100
        for(int i = 0; i < list.length; i++) {
          list[i] = (int)(Math.random()*100 + 1);
        } //End loop


        //Print output
        System.out.println("Generated unsorted list: " + Arrays.toString(list));
        System.out.println("Bubble Sorted Numbers:" + Arrays.toString(bubbleSort(list)));
}


//BubbleSort method provided by textbook
public static void bubbleSort(int[] list) 
  {
    int temp;

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

Your bubbleSort method will sort the array in place, ie it will change the existing array. 您的bubbleSort方法将对数组进行排序,即它将更改现有数组。 Also, it has a void return type, so you cannot pass it as a parameter to Arrays.toString . 另外,它具有void返回类型,因此您不能将其作为参数传递给Arrays.toString

I believe you need to replace this line: 我相信您需要替换此行:

System.out.println("Bubble Sorted Numbers:" + Arrays.toString(bubbleSort(list)));

with these two lines: 这两行:

bubbleSort(list);
System.out.println("Bubble Sorted Numbers:" + Arrays.toString(list));

This way you sort the array first with a call to bubbleSort , and then you pass the same list (which has now been modified by bubbleSort ) to Arrays.toString . 这样,您首先通过调用bubbleSort对数组进行bubbleSort ,然后将同一列表(现已由bubbleSort修改)传递给Arrays.toString

Your bubblesort is of type void and does not return anything. 您的Bubbles类型为void,不返回任何内容。 try to change void to int[] and return your list. 尝试将void更改为int []并返回您的列表。

therefore no value is passed inside your toString method. 因此,没有值传递给您的toString方法内部。

Arrays.toString(bubbleSort(list)));

try doing something like this: 尝试做这样的事情:

public static int[] bubbleSort(int[] list)
  {
    int temp;

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

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

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