You can use randInt to generate the random numbers:
public static int randInt (int low, int high) {
while (true) {
int x = (int)(Math.random() * (high-low+1) + low);
if (x >= low && x <= high) return x;
}
}
You can also use a command-line argument to control which sort
algorithm the program uses. When you run a program using the
Java run-time system, the words
you type after the name of the startup class get put into the array
of Strings that is passed to main. For example, I wrote
the following in main:
if (args[0].charAt(0) == 'm') {
a = mergeSort (a);
} else {
a = selectionSort (a);
}
Then at the UNIX prompt I can write java Sort selection
or java Sort merge and it uses the specified sort algorithm.
% javac Sort.java % time java Sort s % time java Sort mOn my machine, the output is
8.110u 0.070s 0:08.18 100.0% 0+0k 0+0io 2109pf+0wThe first number is ``user time,'' the second is ``system time,'' and the third number is what we want, elapsed time in minutes and seconds.