简体   繁体   English

[Processing 2.0]尝试创建随机整数数组,然后对它们进行冒泡排序

[英][Processing 2.0]Trying to create an array of random integers and then bubble sort them

Im trying to come up with an array of random integers in processing and then use bubble sort to sort them, but I want to print the list of random integers unsorted first and then sort and print, also I wanted the array to be between (1-100). 我试图在处理中提出一个随机整数数组,然后使用冒泡排序对它们进行排序,但是我想先打印未排序的随机整数列表,然后进行排序和打印,我也希望数组位于(1 -100)。 This is the code I wrote attempting this but I think i might be way off. 这是我编写的尝试此操作的代码,但我认为可能还很遥远。 Thanks for any replies. 感谢您的任何答复。

int[] arr = new int[10];


int min  = 1;
int max = 100;
int Random = int(random(min, max));
int R0 = int(random(min, max));
int R1 = int(random(min, max)); 
int R2 = int(random(min, max));
int R3 = int(random(min, max));
int R4 = int(random(min, max));
int R5 = int(random(min, max));
int R6 = int(random(min, max));
int R7 = int(random(min, max));
int R8 = int(random(min, max));
int R9 = int(random(min, max));



println ("The Length of the Array is: " + arr.length);
println ("The " + arr.length + " random numbers chosen between (" + min + " - " + max   + ") are: " + R0 + ", " + R1 + ", " + R2 + ", " + R3 + ", " + R4 + ", " + R5+ ", " + R6 + ", " + R7 + ", " + R8 + ", " + R9 + ".");
for (int i = 0; i <100;  i++)
 {  print((arr[i]= Random + i ) + ", ");

}

EDIT: The final revised version I came up with: 编辑:我想出的最终修订版:

int Random = int(random(10, 100));

int[] array = new int[Random]; // RANDOM SIZE OF ARRAY
int min  = 10; // MINIMUM RANDOM INTEGER CHOSEN
int max = 100; // MAX RANDOM INTEGER CHOSEN

int temp;


for (int i=0; i<array.length; i++) {
  array[i] = (int)random(min,max);
}
println("The Amount of Numbers That Will Be Chosen Is: " + array.length);
println("The Unsorted Numbers Chosen Are Between (" + min + " - " + max + ").");
println();
println("The Numbers Are:");
println(array);
println();
println("The Sorting Begins");


  for (int i = 0; i < array.length; i++)   
{
  println();
  println("Step " + i);  // Prints the step of insertion using "i" as the counter

    for (int j = 0; j < i; j++)
  {
      print(array[j] + ", ");
      if (array[i] < array[j])
    {
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
        }
      }
      println();
   }

First, why are you creating individual values for your randoms. 首先,为什么要为随机数创建单个值。 You should be doing something like this: 您应该这样做:

int[] iArr = new int[50];
for (int i=0; i<iArr.length; i++) {
  iArr[i] = (int)random(min,max);
}

That will create an array of 50 and fill it with random numbers, using your code above. 使用上面的代码,将创建一个由50个数组组成的数组,并用随机数填充。

Next you need to print unsorted: 接下来,您需要打印未排序的:

System.out.println("Unsorted:");
for(int i=0; i<iArr.length; i++) {
  System.out.println(iArr[i]);
}

Then you do your bubble sort, then call the above again to print them in order. 然后您进行气泡排序,然后再次调用上述命令以按顺序打印它们。

also I wanted the array to be between (1-100) 我也希望数组在(1-100)之间

Hint: use Random class which provides a nice function nextInt(int n) function: Returns a pseudorandom, uniformly distributed int value between 0 ( inclusive ) and the specified value n ( exclusive ), drawn from this random number generator's sequence. 提示:使用提供良好函数的RandomnextInt(int n)函数:返回一个伪随机数,它从此随机数生成器的序列得出的int值均匀地分布在0包含 )和指定值nexclusive )之间。

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

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