Reputation: 29
"To this program we will add the quick sort and the merge sort (non recursive)". I'm not sure how to do this with a random array. I formed this code so far, can anyone help?
import java.util.Random; public class Algo {
public static void main(String[] args) {
Random gen = new Random();
int[] a = new int[20];
for (int i = 0; i < a.length; i++)
a[i] = gen.nextInt(100);
printArray(a);
}
private static void printArray(int[] a){
for (int i : a)
System.out.print(i + " ");
System.out.println("");
}
}
}
Upvotes: 2
Views: 175
Reputation: 235994
To generate an array of random elements, try this:
int[] array = new int[20];
Random random = new Random();
for (int i = 0; i < array.length; i++)
array[i] = random.nextInt();
... Afterwards you can work on your merge sort and quick sort algorithms. What have you done so far?
public static void mergeSort(int[] array) {
// sorts the array in-place using merge sort algorithm
}
public static void quickSort(int[] array) {
// sorts the array in-place using quick sort algorithm
}
Upvotes: 1