Part of my assignment requires 3 array of input size 100 with integers from 1-1000. Where 1st array is of random numbers from 1-1000. 2nd is sorted array from integers 1-1000. 3rd is almost sorted array from integers 1-1000.
My random array..
int array [] = new int [100]; // Random Array of 100
for ( int i = 0 ; i < array-1 ; i++) {
array [i] = (int) (Math.random () * 100);
}
My sorted array..
int [] array = new int[100]; // Sorted Array of 100
for (int a = 0; a < array.length; a++) {
array[a] = (a + 1) * 10;
}
But how to do a almost sorted array. I thought about shuffling but that will just be all random.
A faster solution:
int [] array = new int[100]; // Almost sorted Array of 100
array[0] = (int)(Math.random () * 10) + 1;
for (int a = 1; a < array.length; a++) {
array[a] = array[a-1] + (int)(Math.random() * 12) - 2;
}
Hope that's close enough to an "almost sorted" array.
Try this,
int[] values = new int[100];
Random random = new Random();
random.nextInt();
for (int i = 0; i < 100; i++)
{
values[i] = random.nextInt(900) + 100;
}
Arrays.sort(values);
for (int i = 0; i < 100; i++)
{
System.out.println(values[i]);
}
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.