<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">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 &gt;= 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();
        // inv: have done iterations 0..i-1
        for (int i= 0; i &lt; 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();
        
        // inv: have done iterations 0..i-1
        for (int i= 0; i &lt; m; i= i+1) {
            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: put -i in b[i]. */
    public static void fillbneg(int[] b) {
        // inv: have filled in b[0..i-1]
        for (int i= 0; i != b.length; i= i+1)
            b[i]= -i;
    }
    
    /** Fill array b: put i in b[i]. */
    public static void fillbpos(int[] b) {
        // inv: have filled in b[0..i-1]
        for (int i= 0; i != b.length; i= i+1)
            b[i]= i;
    }
    
    /** Execute selection sort and quicksort m times
      on an array of 75000 elements. Print timing results
      in the console. Precondition: m &gt;= 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();
        
        // inv: have done iterations 0..i-1
        for (int i= 0; i &lt; 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 quick sort m times and print the results
        System.out.println("Running quick sort " + m + " times");
        
        timeStart= new Date();
        // inv: have done iterations 0..i-1
        for (int i= 0; i &lt; 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 &gt;= 0. */
    public static void testSearches(int m) {
        
        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();
        int i;  
        // inv: linearSearch has been called i times.
        for (i= 0; i &lt; m; i= i+1) {
            int answer= 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 &lt; 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()));
        
        
    }
    
}
</pre></body></html>