//These 3 functions are all the same function but are timing different sor //ts. In this case if we could tell this function //which sorting function to use, we could just utilize one. Your job is to repace this function with one function that will //Call the appropriate sorting function static void timeSortingRoutine(int [] unsortedData, delegateSorter dTime) { Stopwatch sw = new Stopwatch(); sw.Start(); dTime(unsortedData); sw.Stop(); TimeSpan ts = sw.Elapsed; Console.WriteLine("Time elapsed {0} milliseconds.", ts.TotalMilliseconds); }
//////////////////////////////////////////////////////////////////////////////// static void Main(string[] args) { string[] data = File.ReadAllLines("..\\..\\input.txt"); int[] intdata = new int[data.Length]; int count = 0; foreach (string s in data) { intdata[count] = Convert.ToInt32(s); count++; } int[] copyData = new int[intdata.Length]; reset(intdata, copyData); //In this section we are calling the three different methods. Instead we need to use delegates to create 3 //pointers to the different sorting routine and passing them to one method to run the sorting mechanism. delegateSorter dBubble = new delegateSorter(bubblesort); delegateSorter dSelection = new delegateSorter(selectionSort); delegateSorter dQuick = new delegateSorter(Quicksort_Starter); Console.WriteLine("Bubble Sort"); timeSortingRoutine(copyData, dBubble); reset(intdata, copyData); Console.WriteLine("Selection Sort"); timeSortingRoutine(copyData, dSelection); reset(intdata, copyData); Console.WriteLine("Quick Sort"); timeSortingRoutine(copyData, dQuick); ; Console.WriteLine("Press Enter to exit"); Console.ReadLine(); }