import java.util.*;

/** Program for Lab: Timing execution */
public class TestArrays {
   
   /** Determine and print how long it takes to create a Date object. */
   public static void times() {
      // Store in timeStart a Date with the time at which the statement is executed.
      Date timeStart= new Date();
      
      // Store in timeEnd a Date with the time at which the statement is executed.
      Date timeEnd= new Date();
      
      System.out.println(timeStart);
      System.out.println("Time timeStart, in milliseconds: " + timeStart.getTime());
      System.out.println();
      System.out.println(timeEnd);
      System.out.println("Time timeStart, in milliseconds: " + timeEnd.getTime());
   }
   
   /** Execute selection sort and insertion sort m times
       on an array of 5000 elements. Print timing results
       in the console. Precondition: m >= 0*/
   public static void testSorts(int m) {
      Date timeStart;
      Date timeEnd;      
      
      int[] b= new int[5000]; // array to sort
      
      
      // Run selection sort m times and print the results
         System.out.println("Running selection sort " + m + " times");

         timeStart= new Date();
         for (int i= 0; i < m; i= i+1) {
            fillbneg(b);
            Sorting.selectionSort(b);
         }
         timeEnd= new Date();
      
         System.out.println("Time for selection sort: " +
                            (int)(timeEnd.getTime()-timeStart.getTime()));
        
         
       // Run insertion sort m times and print the results
         System.out.println("Running insertion sort " + m + " times");
         timeStart= new Date();
            
          for (int i= 0; i < m; i++) {
             fillbneg(b);
             Sorting.insertionSort(b,0,b.length-1);
          }     
          timeEnd= new Date();
       
          System.out.println("Time for insertion sort: " +
                            (int)(timeEnd.getTime()-timeStart.getTime()));
   }
   
      
   // Fill array b: in b[i] goes -i
   public static void fillbneg(int[] b) {
      for (int i= 0; i != b.length; i++)
          b[i]= -i;
   }
   
     // Fill array b: in b[i] goes i
   public static void fillbpos(int[] b) {
      for (int i= 0; i != b.length; i++)
          b[i]= i;
   }
   
   /** Execute selection sort and quicksort m times
       on an array of 75000 elements. Print timing results
       in the console. Precondition: m >= 0*/
   public static void testSorts2(int m) {
      Date timeStart;
      Date timeEnd;      
      
      int[] b= new int[75000]; // array to sort  
         
      // Run selection sort m times and print the results
         System.out.println("Running selection sort " + m + " times");
         timeStart= new Date();
            
          for (int i= 0; i < m; i++) {
             fillbneg(b);
             Sorting.selectionSort(b);
          }     
          timeEnd= new Date();
       
          System.out.println("Time for selection sort: " +
                            (int)(timeEnd.getTime()-timeStart.getTime()));
          
      // Run quick sort m times and print the results
         System.out.println("Running quick sort " + m + " times");

         timeStart= new Date();
         for (int i= 0; i < m; i= i+1) {
            fillbneg(b);
            Sorting.quicksort(b, 0, b.length-1);
         }
         timeEnd= new Date();
      
         System.out.println("Time for quicksort sort: " +
                            (int)(timeEnd.getTime()-timeStart.getTime()));
      
   }
   
   // Execute linear search and binary search m times
   // on an array of 100000 elements. Print results in
   // the console. Precondition: m >= 0
   public static void testSearches(int m) {
      int i; // = number of times a search 
      
      Date timeStart;
      Date timeEnd;
      
      int[] b= new int[100000]; // array to sort
      
      // Run linear search m times and print the results
         System.out.println("Running linear search " + m + " times");
         
         fillbpos(b);
         timeStart= new Date();
         // inv: linearSearch has been called i times.
         for (i= 0; i < m; i= i+1) {
            int anser= Sorting.linearSearch(b,b.length);
         }
         timeEnd= new Date();
      
         System.out.println("Time for linear search: " +
                            (int)(timeEnd.getTime()-timeStart.getTime()));
        
         
       // Run binary search m times and print the results
          System.out.println("Running binary search sort " + m + " times");
          fillbpos(b);
          timeStart= new Date();
          // inv: binarySearch has been called i times.  
          for (i= 0; i < m; i= i+1) {
             int bs= Sorting.binarySearch(b,b.length);
          }     
          timeEnd= new Date();
       
          System.out.println("Time for binary search: " + (int)(timeEnd.getTime()-timeStart.getTime()));
        

   }
  
}
