简体   繁体   中英

[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). 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.

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)

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.

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