public void Test3() { var list = new List <int> { 5, 8, 4, 1, 2, 6, 4, 7, 9, 2 }; HeapSorter.HeapSortDescending <int>(list, null); }
/// <summary> /// Class constructor /// </summary> /// <param name="collection">A list</param> /// <param name="comparer">A comparer</param> public BinarySearcher(IList <T> collection, Comparer <T> comparer) { if (collection == null) { throw new NullReferenceException("List is null"); } _collection = collection; _comparer = comparer; HeapSorter.HeapSort(_collection); }
public void SortDescending_SortsData(List <int> data) { // Arrange var heapSorter = new HeapSorter <int>(); // Act var sortedData = heapSorter.SortDescending((IEnumerable <int>)data); // Assert Assert.Equal(data.OrderByDescending(i => i), sortedData); }
public Researcher(BubleSorter bubleSorter, ShellSorter shellSorter, QuickSorter quickSorter, MergeSorter mergeSorter, HeapSorter heapSorter, RadixSorter radixSorter, BuiltInSorter builtInSorter, DataGenerator dataGenerator, IOptions <ResearcherSettings> options) { _dataGenerator = dataGenerator; _settings = options.Value; _sorters = new Sorter[] { bubleSorter, shellSorter, quickSorter, mergeSorter, heapSorter, radixSorter, builtInSorter }; }
public void SortExample() { var sorter = new HeapSorter<int>(); var list = new List<int> {13, 5, 77, 9, 12}; sorter.Sort(list, Comparer<int>.Default); Assert.AreEqual(5, list[0]); Assert.AreEqual(9, list[1]); Assert.AreEqual(12, list[2]); Assert.AreEqual(13, list[3]); Assert.AreEqual(77, list[4]); }
/// <summary> /// Обработчик события установки флажка на Heap sorter /// </summary> /// <param name="sender">объект инициатор события<</param> /// <param name="e">параметры события</param> private void rBtnHeap_CheckedChanged(object sender, EventArgs e) { if (rBtnHeap.Checked) { _sort = new HeapSorter(_arr); _sort.SortingStart += StartSortMessage; _sort.Swapp += SwapCounting; _sort.Compare += CompareCounting; _sort.SortingFin += FinishSortMessage; } else { _sort = null; } }
public void Sort_DoesNotChange_OriginalInput() { //arrange const int firstElement = 5, lastElement = 3; var original = new List <int> { firstElement, 2, 4, 6, 1, lastElement }; var sorter = new HeapSorter <int>(); //act var sorted = sorter.Sort(original); //assert original[0].Should().Be(firstElement); original[5].Should().Be(lastElement); }
public static void ArraySorted([Random(0, 1000, 100, Distinct = true)] int n) { // Arrange var sorter = new HeapSorter <int>(); var intComparer = new IntComparer(); var(correctArray, testArray) = RandomHelper.GetArrays(n); // Act sorter.Sort(testArray, intComparer); Array.Sort(correctArray); // Assert Assert.AreEqual(correctArray, testArray); }
public void SortExample() { var sorter = new HeapSorter <int>(); var list = new List <int> { 13, 5, 77, 9, 12 }; sorter.Sort(list, Comparer <int> .Default); Assert.AreEqual(5, list[0]); Assert.AreEqual(9, list[1]); Assert.AreEqual(12, list[2]); Assert.AreEqual(13, list[3]); Assert.AreEqual(77, list[4]); }
static void SortInts() { Console.WriteLine("对整型数组排序:"); int[] intArray = { 12, 2, 34, 15, 23, 11, 6, 9, 11 }; HeapSorter<int> intSorter = new HeapSorter<int>(intArray, CompareInt); Console.WriteLine("排序之前的整型数组:"); foreach ( int num in intArray ) { Console.Write(num + " "); } Console.WriteLine("\n排序之后的整型数组:"); foreach ( int num in intSorter.ArrayAfterSort() ) { Console.Write(num + " "); } }
static void SortStrings() { Console.WriteLine("对字符串数组排序:"); string[] strArray = { "zpasd", "qwe", "wer", "fgdg", "ertw", "qrtre" }; HeapSorter<string> strSorter = new HeapSorter<string>(strArray, CompareString); Console.WriteLine("排序之前的字符串数组:"); foreach ( string str in strArray ) { Console.Write(str + " "); } Console.WriteLine("\n排序之后的字符串数组:"); foreach ( string str in strSorter.ArrayAfterSort() ) { Console.Write(str + " "); } }
public void Sort_Returns_AscOrderedCollection() { //arrange var original = new List <int> { 5, 2, 4, 6, 1, 3 }; var sorter = new HeapSorter <int>(); //act var sorted = sorter.Sort(original).ToList(); //assert var prev = sorted[0]; for (var i = 1; i < sorted.Count; i++) { prev.Should().BeLessOrEqualTo(sorted[i]); prev = sorted[i]; } }
/// <summary> /// 堆排序 /// </summary> /// <param name="i"></param> public void HeapSorter(int i = 0) { Action <bool> action = (p) => { IList <int> list2 = list; HeapSorter <int> .Sort(list, true); if (p) { Trace.WriteLine("堆排序:"); Trace.WriteLine(list2.ToJson()); } }; if (i < 1) { action(true); } else { string data = ActionExtensions.Time(() => action(false), "堆排序", i); Trace.WriteLine(data.Replace("<br />", Environment.NewLine)); } }
public void HeapSorter_Sort_Check_Sorting_Behavior() { int[] unsortedInts = { 6, 12, 8, 2, 9 }; HeapSorter.HeapSort(unsortedInts); }
public void ExceptionNullList3() { var sorter = new HeapSorter<int>(); sorter.Sort(null, SortOrder.Ascending); }
public void ExceptionNullComparer1() { var sorter = new HeapSorter<int>(); sorter.Sort(new List<int>(), (IComparer<int>)null); }
public void ExceptionNullList1() { var sorter = new HeapSorter<int>(); sorter.Sort(null); }
public void ExceptionNullList2() { var sorter = new HeapSorter<int>(); sorter.Sort(null, Comparer<int>.Default); }
public void Simple() { var sorter = new HeapSorter<int>(); TestSorter(sorter); }
public void HeapSorter_Duplicates_Test() { int[] unsortedInts = { 6, 12, 8, 2, 9, 6 }; HeapSorter.HeapSort(unsortedInts); }
/// <summary> /// Sorts the specified column in the specified sort direction /// </summary> /// <param name="index">The index of the column to sort</param> /// <param name="column">The column to sort</param> /// <param name="sortOrder">The direction the column is to be sorted</param> /// <param name="stable">Specifies whether a stable sorting method /// should be used to sort the column</param> private void Sort(int index, Column column, SortOrder sortOrder, bool stable) { // make sure a null comparer type doesn't sneak past ComparerBase comparer = null; if (column.Comparer != null) { comparer = (ComparerBase)Activator.CreateInstance(column.Comparer, new object[] { this.TableModel, index, sortOrder }); } else if (column.DefaultComparerType != null) { comparer = (ComparerBase)Activator.CreateInstance(column.DefaultComparerType, new object[] { this.TableModel, index, sortOrder }); } else { return; } column.InternalSortOrder = sortOrder; // create the comparer SorterBase sorter = null; // work out which sort method to use. // - InsertionSort/MergeSort are stable sorts, // whereas ShellSort/HeapSort are unstable // - InsertionSort/ShellSort are faster than // MergeSort/HeapSort on small lists and slower // on large lists // so we choose based on the size of the list and // whether the user wants a stable sort if (this.SortType == SortType.AutoSort) { if (this.TableModel.Rows.Count < 1000) { if (this.StableSort) sorter = new InsertionSorter(this.TableModel, index, comparer, sortOrder); else sorter = new ShellSorter(this.TableModel, index, comparer, sortOrder); } else { if (this.StableSort) sorter = new MergeSorter(this.TableModel, index, comparer, sortOrder); else sorter = new HeapSorter(this.TableModel, index, comparer, sortOrder); } } else { switch (this.SortType) { case SortType.HeapSort: { sorter = new HeapSorter(this.TableModel, index, comparer, sortOrder); break; } case SortType.InsertionSort: { sorter = new InsertionSorter(this.TableModel, index, comparer, sortOrder); break; } case SortType.MergeSort: { sorter = new MergeSorter(this.TableModel, index, comparer, sortOrder); break; } case SortType.ShellSort: { sorter = new ShellSorter(this.TableModel, index, comparer, sortOrder); break; } default: { throw new ApplicationException("Invalid Sort Type - " + this.SortType.ToString()); } } } sorter.SecondarySortOrders = this.ColumnModel.SecondarySortOrders; sorter.SecondaryComparers = this.GetSecondaryComparers(this.ColumnModel.SecondarySortOrders); // don't let the table redraw this.BeginUpdate(); this.OnBeginSort(new ColumnEventArgs(column, index, ColumnEventType.Sorting, null)); // Added by -= Micronn =- on 22 dec 2005 Row focusedRow = null; if (this.FocusedCell != CellPos.Empty) { focusedRow = this.tableModel.Rows[this.FocusedCell.Row]; } sorter.Sort(); // Added by -= Micronn =- on 22 dec 2005 if (focusedRow != null) { this.FocusedCell = new CellPos(focusedRow.Index, this.FocusedCell.Column); this.EnsureVisible(this.FocusedCell); } this.OnEndSort(new ColumnEventArgs(column, index, ColumnEventType.Sorting, null)); // redraw any changes this.EndUpdate(); }
public void TestHeapSorter() { HeapSorter <int> sorter = new HeapSorter <int>(); TestSorter(sorter); }
public void TestHeapSortNullList1() { HeapSorter <int> sorter = new HeapSorter <int>(); sorter.Sort(null); }
public void TestHeapSortNullList2() { HeapSorter <int> sorter = new HeapSorter <int>(); sorter.Sort(null, Comparer <int> .Default); }
public void TestHeapSortNullComparer1() { HeapSorter <int> sorter = new HeapSorter <int>(); sorter.Sort(new List <int>(), (IComparer <int>)null); }
public void TestHeapSortNullList3() { HeapSorter <int> sorter = new HeapSorter <int>(); sorter.Sort(null, SortOrder.Ascending); }
public static void Main() { int[] iArray = new int[] { 11, 25, 33, 6, 30, 75, 29, 2, 107, 32, 54, 95, 43, 67 }; HeapSorter hs = new HeapSorter(ref iArray); MainClass.showArray(iArray); hs.sortArray(); MainClass.showArray(iArray); }