MergeSortTimer.java.html

  1  import java.util.Scanner;
  2  
  3  /**
  4     This program measures how long it takes to sort an
  5     array of a user-specified size with the merge
  6     sort algorithm.
  7  */
  8  public class MergeSortTimer
  9  {  
 10     public static void main(String[] args)
 11     {  
 12        Scanner in = new Scanner(System.in);
 13        System.out.print("Enter array size: ");
 14        int n = in.nextInt();
 15  
 16        // Construct random array
 17     
 18        int[] a = ArrayUtil.randomIntArray(n, 100);
 19        
 20        // Use stopwatch to time merge sort
 21  
 22        StopWatch timer = new StopWatch();
 23  
 24        timer.start();
 25        MergeSorter.sort(a);
 26        timer.stop();
 27  
 28        System.out.println("Elapsed time: " 
 29              + timer.getElapsedTime() + " milliseconds");
 30     }
 31  }
 32  
 33