Java Reference
In-Depth Information
6
sort algorithm.
7 */
8
public class
SelectionSortTimer
9 {
10
public static void
main(String[] args)
11 {
12 Scanner in =
new
Scanner(System.in);
13 System.out.print(
ÐE ter array size: Ñ
);
14
int
n = in.nextInt();
15
16//
Construct random array
17
18
int
[] a = ArrayUtil.randomIntArray(n,
100
);
19 SelectionSorter sorter =
new
SelectionSorter(a) ;
20
21//
Use stopwatch to time selection sort
22
23 StopWatch timer =
new
StopWatch();
24
25 timer.start();
26 sorter.sort();
27 timer.stop();
28
29 System.out.println(
ÐElapsed time: Ñ
30 + timer.getElapsedTime() +
ÐmillisecondsÑ
);
31 }
32 }
633
634
Output
Enter array size:
100000
Elapsed time: 27880 milliseconds
By starting to measure the time just before sorting, and stopping the stopwatch just
after, you don't count the time it takes to initialize the array or the time during which
the program waits for the user to type in
n
.
Here are the results of some sample runs: