/// <summary> /// Sort function which takes the sorting function to use and sorts the entry list. /// </summary> /// <param name="sort"></param> public void Sort(SortingFunction sort) { entries.Sort((x, y) => sort(x, y)); for (int i = 0; i < entries.Count; i++) { entries[i].transform.SetSiblingIndex(i); } }
public void Test(SortingMethodDelegate SortingMethod, Series serie, string sSortingMethod) { int[] iTempArray = new int[iArray.Length]; iArray.CopyTo(iTempArray, 0); SortingFunction SortingFunction = new SortingFunction(); SortingFunction.iArray = iTempArray; SortingFunction.SortingMethod = SortingMethod; SortingFunction.sSortingMethod = sSortingMethod; Thread thread = new Thread(SortingFunction.Sorting); thread.Priority = ThreadPriority.Highest; thread.Start(); Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256)); if (!thread.Join((int)numericUpDown4.Value)) { thread.Abort(); serie.Points.AddXY(sSortingMethod, (double)numericUpDown4.Value); serie.Points[serie.Points.Count - 1].Label = "more than " + numericUpDown4.Value.ToString(); serie.Points[serie.Points.Count - 1].Color = randomColor; chart1.Legends["Legend"].CustomItems.Add(randomColor, sSortingMethod); Thread OutputThread2 = new Thread(SortingFunction.OutputToFile); OutputThread2.Priority = ThreadPriority.Normal; OutputThread2.Start(); return; } else if (isCorrectlySorted(iTempArray)) { throw new InvalidOperationException("Array isn't sorted!"); } Thread OutputThread = new Thread(SortingFunction.OutputToFile); OutputThread.Priority = ThreadPriority.Normal; OutputThread.Start(); serie.Points.AddXY(sSortingMethod, SortingFunction.Time); serie.Points[serie.Points.Count - 1].Label = SortingFunction.Time.ToString(); serie.Points[serie.Points.Count - 1].Color = randomColor; chart1.Legends["Legend"].CustomItems.Add(randomColor, sSortingMethod); }
static void Main(string[] args) { // declare the unsorted and sorted arrays int[] aUnsorted; int[] aSorted; SortingFunction processLowHigh; // a label to allow us to easily loop back to the start if there are input issues start: Console.WriteLine("Enter a list of space-separated numbers"); // read the space-separated string of numbers string sNumbers = Console.ReadLine(); // split the string into the an array of strings which are the individual numbers string[] sNumber = sNumbers.Split(' '); // initialize the size of the unsorted array to 0 int nUnsortedLength = 0; // a double used for parsing the current array element int nThisNumber; // iterate through the array of number strings foreach (string sThisNumber in sNumber) { // if the length of this string is 0 (ie. they typed 2 spaces in a row) if (sThisNumber.Length == 0) { // skip it continue; } try { // try to parse the current string into a double nThisNumber = int.Parse(sThisNumber); // if it's successful, increment the number of unsorted numbers ++nUnsortedLength; } catch { // if an exception occurs // indicate which number is invalid Console.WriteLine($"Number #{nUnsortedLength + 1} is not a valid number."); // loop back to the start goto start; } } // now we know how many unsorted numbers there are // allocate the size of the unsorted array aUnsorted = new int[nUnsortedLength]; // reset nUnsortedLength back to 0 to use as the index to store the numbers in the unsorted array nUnsortedLength = 0; foreach (string sThisNumber in sNumber) { // still skip the blank strings if (sThisNumber.Length == 0) { continue; } // parse it into a double (we know they are all valid now) nThisNumber = int.Parse(sThisNumber); // store the value into the array aUnsorted[nUnsortedLength] = nThisNumber; // increment the array index nUnsortedLength++; } // allocate the size of the sorted array aSorted = new int[nUnsortedLength]; // start the sorted length at 0 to use as sorted index element int nSortedLength = 0; Console.Write("Sort in <a>scending or <d>escending order: "); string sAscDesc = Console.ReadLine(); ///************************************************************ /// delegate steps /// 1. define the delegate method data type /// delegate double MathFunction(double n1, double n2); /// 2. allocate the delegate method variable /// MathFunction processDivMult; /// 3. point the variable to the method that it should call /// processDivMult = new MathFunction(Multiply); /// 4. call the delegate method /// nAnswer = processDivMult(n1, n2); ///************************************************************ // 1. Create FindHighestValue() based on FindLowestValue() // 2. define the delegate method data type // called SortingFunction with the correct method signature // 3. declare a delegate method variable of type SortingFunction if (sAscDesc.ToLower().StartsWith("a")) { // 4. if ascending, then set the delegate variable to FindLowestValue processLowHigh = new SortingFunction(FindLowestValue); } else { // 5. else set the delegate variable to FindHighestValue processLowHigh = new SortingFunction(FindHighestValue); } // while there are unsorted values to sort while (aUnsorted.Length > 0) { // 6. call the delegate method aSorted[nSortedLength] = processLowHigh(aUnsorted); RemoveUnsortedValue(aSorted[nSortedLength], ref aUnsorted); ++nSortedLength; } // write the sorted array of numbers Console.WriteLine("The sorted list is: "); foreach (double thisNum in aSorted) { Console.Write($"{thisNum} "); } Console.WriteLine(); }
private bool SortringFunctionTest(SortingFunction sortingFunction, int length) { long[] actual = Program.GenerateRandomNumbersArray(length, length); var expected = new long[actual.Length]; actual.CopyTo(expected, 0); _standartSortingFunction(expected); sortingFunction(actual); return AreArraysEqual(expected, actual); }