/// <summary> /// 按照指定的门限删除数据 /// </summary> /// <param name="new_data">需要处理的数据</param> /// <param name="k">目标门限值</param> /// <param name="flagNumbers">数据在的下标位置</param> public void GetThresholdDatas(double[] new_data, double k, string max, out int[] flagNumbers) { double[] data_ = new double[new_data.Length]; Array.Copy(new_data, data_, new_data.Length); data_ = ShellSorter.Sort(data_); double sd; while (true) { sd = this.GetOffect(data_); List <double> list = data_.ToList(); if (max == "max") { list.Remove(this.GetMax(data_)); } else { list.Remove(this.GetMin(data_)); } double[] remain_data = new double[list.Count]; Array.Copy(list.ToArray(), remain_data, remain_data.Length); sd = this.GetOffect(remain_data); data_ = list.ToArray(); if (sd <= k) { double[] remainDatas; GetDifferentDatas(new_data, data_, out flagNumbers, out remainDatas); break; } } }
public void TestFirstAndLastSort() { short[] data = { 10, 7, 3, 1, 9, 7, 4, 3 }; ShellSorter.Sort <short>(data, ShortComparer); Assert.Equal((short)1, data[0]); Assert.Equal((short)10, data[7]); }
/// <summary> /// 从数组最小值开始移除元素,直至标准偏差达到指定值 /// </summary> /// <param name="data"></param> /// <param name="k"></param> /// <param name="new_sd_min"></param> /// <param name="remain"></param> public void DelErroData_Min(double[] data, double k, out double new_sd_min, out double[] remain) { if (k < 0) { throw new Exception("k is erro"); } data = ShellSorter.Sort(data); double sd = this.GetOffect(data); int i = 1; while (true) { if (sd < k) { new_sd_min = sd; remain = data; break; } else { double[] remain_data = new double[data.Length - i]; Array.Copy(data, i, remain_data, 0, remain_data.Length); sd = this.GetOffect(remain_data); i++; if (sd < k) { new_sd_min = sd; remain = remain_data; break; } } } }
public static void Main(string[] args) { Console.WriteLine("Hello! Preparing to sort data."); short[] data = { 10, 7, 3, 1, 9, 7, 4, 3 }; ShellSorter.Sort <short>(data, ShortComparer); Console.WriteLine("Finished sorting data. The array contents are: "); Console.WriteLine(string.Join(",", data)); }
public static void ArraySorted([Random(0, 1000, 100, Distinct = true)] int n) { // Arrange var sorter = new ShellSorter <int>(); var intComparer = new IntComparer(); var(correctArray, testArray) = RandomHelper.GetArrays(n); // Act sorter.Sort(testArray, intComparer); Array.Sort(correctArray, intComparer); // Assert Assert.AreEqual(testArray, correctArray); }
/// <summary> /// 按照比例移除部分数据 /// 上下移除 /// </summary> /// <param name="data">原始数据</param> /// <param name="k">移除比例</param> /// <param name="remain">剩下的数据</param> public void RemoveProportionData(double[] data, double k, out double[] remain) { if (k < 0)//移除系数异常 { throw new Exception("k is erro"); } int len = Convert.ToInt32(Math.Round(k)) * data.Length;//需要移除的数据长度 data = ShellSorter.Sort(data); List <double> dataList = data.ToList(); dataList.RemoveRange(0, len); dataList.RemoveRange(dataList.Count - len - 1, len); remain = dataList.ToArray(); }
/// <summary> /// 移除指定的数据后返回一个新的数据 /// 按照最大最小个移除指定的数据 /// </summary> /// <param name="data"></param> /// <param name="new_data"></param> /// <param name="max">指示是否从最大值处开始移除</param> public void GetRemoveNumData(double[] data, out double[] new_data, bool max) { int k = (int)Math.Round(data.Length * 0.1); int num = data.Length; data = ShellSorter.Sort(data); List <double> list = data.ToList(); for (int i = 0; i < k; i++) { if (max == true) { list.RemoveAt(list.Count - 1); } else { list.RemoveAt(0); } } new_data = list.ToArray(); }
public void ArraySorted([Random(0, 1000, 1000)] int n) { // Arrange var sorter = new ShellSorter <int>(); var intComparer = new IntComparer(); var random = new Random(); var testArray = new int[n]; var correctArray = new int[n]; for (var i = 0; i < n; i++) { var t = random.Next(0, 1000); testArray[i] = t; correctArray[i] = t; } // Act sorter.Sort(testArray, intComparer); Array.Sort(correctArray, intComparer); // Assert Assert.AreEqual(testArray, correctArray); }
public void TestShellSortNullList2() { ShellSorter <int> sorter = new ShellSorter <int>(); sorter.Sort(null, Comparer <int> .Default); }
public void ExceptionNullComparer1() { var sorter = new ShellSorter<int>(); sorter.Sort(new List<int>(), (IComparer<int>)null); }
public void ExceptionNullList3() { var sorter = new ShellSorter<int>(); sorter.Sort(null, SortOrder.Ascending); }
public void ExceptionNullList2() { var sorter = new ShellSorter<int>(); sorter.Sort(null, Comparer<int>.Default); }
public void ExceptionNullList1() { var sorter = new ShellSorter<int>(); sorter.Sort(null); }
public void TestShellSortNullComparer1() { ShellSorter <int> sorter = new ShellSorter <int>(); sorter.Sort(new List <int>(), (IComparer <int>)null); }
public void TestShellSortNullList3() { ShellSorter <int> sorter = new ShellSorter <int>(); sorter.Sort(null, SortOrder.Ascending); }
public void TestShellSortNullList1() { ShellSorter <int> sorter = new ShellSorter <int>(); sorter.Sort(null); }
static void Main(string[] args) { Console.WriteLine("请选择算法:"); Console.WriteLine("1.选择排序"); Console.WriteLine("2.冒泡排序"); Console.WriteLine("3.高速排序"); Console.WriteLine("4.插入排序"); Console.WriteLine("5.希尔排序"); Console.WriteLine("6.归并排序"); Console.WriteLine("7.基数排序"); Console.WriteLine("8.计数排序"); Console.WriteLine("9.堆排序"); Console.WriteLine("0.退出"); int type = Convert.ToInt32(Console.ReadLine()); string typename = ""; switch (type) { case 1: //选择排序 typename = "选择排序"; break; case 2: //冒泡排序 typename = "冒泡排序"; break; case 3: //高速排序 typename = "高速排序"; break; case 4: //插入排序 typename = "插入排序"; break; case 5: //希尔排序 typename = "希尔排序"; break; case 6: //归并排序 typename = "归并排序"; break; case 7: //基数排序 typename = "基数排序"; break; case 8: //计数排序 typename = "计数排序"; break; case 9: //堆排序 typename = "堆排序"; break; default: typename = "您未选择算法"; break; } Console.WriteLine("您选择了{0}.{1}:", type, typename); int[] arrInt = new int[] { 4, 2, 7, 1, 8, 3, 9, 0, 5, 6 }; int[] intArray = new int[] { 5, 3, 7, 4, 8, 2, 9, 1, 0, 6 }; int[] newIntArray = intArray; switch (type) { case 1: //选择排序 SelectionSorter selSor = new SelectionSorter(); selSor.Sort(arrInt); foreach (int i in arrInt) { Console.WriteLine(i); } Console.ReadKey(); break; case 2: //冒泡排序 EbullitionSorter ebuSor = new EbullitionSorter(); ebuSor.Sort(arrInt); foreach (int i in arrInt) { Console.WriteLine(i); } Console.ReadKey(); break; case 3: //高速排序 QuickSorter quiSor = new QuickSorter(); quiSor.Sort(arrInt, 0, arrInt.Length - 1); foreach (int i in arrInt) { Console.WriteLine(i); } Console.ReadKey(); break; case 4: //插入排序 InsertionSorter insSor = new InsertionSorter(); insSor.Sort(arrInt); foreach (int i in arrInt) { Console.WriteLine(i); } Console.ReadKey(); break; case 5: //希尔排序 ShellSorter sheSor = new ShellSorter(); sheSor.Sort(arrInt); foreach (int i in arrInt) { Console.WriteLine(i); } Console.ReadKey(); break; case 6: //归并排序 while (true) { Console.WriteLine("请选择:"); Console.WriteLine("1.归并排序(非递归)"); Console.WriteLine("2.归并排序(递归)"); Console.WriteLine("3.归并排序(自然合并)"); Console.WriteLine("4.退出"); int Arraynum = Convert.ToInt32(Console.ReadLine()); switch (Arraynum) { case 4: Environment.Exit(0); break; case 1: Console.WriteLine("Please Input Array Length"); int Leng271 = Convert.ToInt32(Console.ReadLine()); Function obj1 = new Function(Leng271); Console.WriteLine("The original sequence:"); Console.WriteLine(obj1); Console.WriteLine("'MergeSort' Finaly Sorting Result:"); obj1.ToMergeSort(); Console.WriteLine(obj1); break; case 2: Console.WriteLine("Please Input Array Length"); int Leng272 = Convert.ToInt32(Console.ReadLine()); Function obj2 = new Function(Leng272); Console.WriteLine("The original sequence:"); Console.WriteLine(obj2); Console.WriteLine("'RecursiveMergeSort' Finaly Sorting Result:"); obj2.ToRecursiveMergeSort(); Console.WriteLine(obj2); break; case 3: Console.WriteLine("Please Input Array Length"); int Leng273 = Convert.ToInt32(Console.ReadLine()); Function obj3 = new Function(Leng273); Console.WriteLine("The original sequence:"); Console.WriteLine(obj3); obj3.ToNaturalMergeSort(); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("'NaturalMergeSort' Finaly Sorting Result:"); Console.WriteLine(obj3); break; } } case 7: //基数排序 RadixSorter rS = new RadixSorter(); newIntArray = rS.RadixSort(intArray, intArray.Length); foreach (int i in intArray) { Console.Write(i + " "); } Console.ReadKey(); break; case 8: //计数排序 int[] intNewArray = intArray; intNewArray = Counting.CountingSort(intArray, intArray.Length); foreach (int i in intNewArray) { Console.Write(i + " "); } Console.ReadKey(); break; case 9: //堆排序 HeapSort.HeapSortFunction(intArray); foreach (int i in intArray) { Console.Write(i + " "); } Console.ReadKey(); break; default: Environment.Exit(0); break; } }
static void SortDemo() { // seven custom algorithms; // for each algorithm prepare copies of the same array to sort Random rd = new Random(); int[] z1 = new int[50000]; for (int i = 0; i < z1.Length; i++) { z1[i] = rd.Next(0, z1.Length); } int[] z2 = new int[z1.Length]; z1.CopyTo(z2, 0); int[] z3 = new int[z1.Length]; z1.CopyTo(z3, 0); int[] z4 = new int[z1.Length]; z1.CopyTo(z4, 0); int[] z5 = new int[z1.Length]; z1.CopyTo(z5, 0); int[] z6 = new int[z1.Length]; z1.CopyTo(z6, 0); int[] z7 = new int[z1.Length]; z1.CopyTo(z7, 0); // ---------------------------------------------------------- Console.WriteLine("Selection Sort:"); ISorter <int> sorter = new SelectionSorter(); DateTime dt1 = DateTime.Now; sorter.Sort(z1, 0, z1.Length - 1); DateTime dt2 = DateTime.Now; Console.WriteLine("Time: {0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z1.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.WriteLine("Bubble sort"); sorter = new BubbleSorter(); dt1 = DateTime.Now; sorter.Sort(z2, 0, z1.Length - 1); dt2 = DateTime.Now; Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z2.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.WriteLine("Insertion sort"); sorter = new InsertionSorter(); dt1 = DateTime.Now; sorter.Sort(z3, 0, z1.Length - 1); dt2 = DateTime.Now; Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z3.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.WriteLine("Shaker sort"); sorter = new ShakerSorter(); dt1 = DateTime.Now; sorter.Sort(z4, 0, z1.Length - 1); dt2 = DateTime.Now; Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z4.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.WriteLine("Shell sort"); sorter = new ShellSorter(); dt1 = DateTime.Now; sorter.Sort(z5, 0, z1.Length - 1); dt2 = DateTime.Now; Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z5.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.WriteLine("Counting sort"); sorter = new CountingSorter(); dt1 = DateTime.Now; sorter.Sort(z6, 0, z1.Length - 1); dt2 = DateTime.Now; Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z6.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.WriteLine("Quicksort"); sorter = new QuickSorter(); dt1 = DateTime.Now; sorter.Sort(z7, 0, z1.Length - 1); dt2 = DateTime.Now; Console.WriteLine("{0}:{1}", (dt2 - dt1).Seconds, (dt2 - dt1).Milliseconds); Console.WriteLine("First 20 elements:"); foreach (int elem in z7.Take(20)) { Console.Write(elem + " "); } Console.WriteLine(); Console.ReadKey(); Console.Clear(); }