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 timeEnd, 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[10000]; // 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 < m; i= i+1) { fillRand(b); Sorting.selectionSort(b); } timeEnd= new Date(); System.out.println("Time for selection sort: " + (int)(timeEnd.getTime()-timeStart.getTime()) + " ms"); // 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 < m; i= i+1) { fillRand(b); Sorting.insertionSort(b,0,b.length-1); } timeEnd= new Date(); System.out.println("Time for insertion sort: " + (int)(timeEnd.getTime()-timeStart.getTime()) + " ms"); } /** Fill array b with random integers in [0,1000000). */ public static void fillRand(int[] b) { // inv: have filled in b[0..i-1] for (int i= 0; i != b.length; i= i+1) b[i]= (int) (Math.random() * 1000000); } /** Fill array b: put -i in b[i]. */ public static void fillNeg(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 fillPos(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 >= 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 < m; i= i+1) { fillRand(b); Sorting.selectionSort(b); } timeEnd= new Date(); System.out.println("Time for selection sort: " + (int)(timeEnd.getTime()-timeStart.getTime()) + " ms"); // 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 < m; i= i+1) { fillRand(b); Sorting.quickSort(b, 0, b.length-1); } timeEnd= new Date(); System.out.println("Time for quicksort sort: " + (int)(timeEnd.getTime()-timeStart.getTime()) + " ms"); } /** 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) { Date timeStart; Date timeEnd; int[] b= new int[1000000]; // array to sort // Run linear search m times and print the results System.out.println("Running linear search " + m + " times"); fillPos(b); timeStart= new Date(); int i; // inv: linearSearch has been called i times. for (i= 0; i < 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()) + " ms"); // Run binary search m times and print the results System.out.println("Running binary search sort " + m + " times"); fillPos(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()) + " ms"); } }