public void HeapSortEmptyArray() { int[] a = new int[0]; int[] expectedOutput = a.Clone() as int[]; HeapSort.Sort(a); Assert.IsTrue(ArrayUtility.AreArraysEqual(a, expectedOutput)); }
private void metroButton7_Click(object sender, EventArgs e) { using (var dbb = new OtelDbcontext()) { var query = from a in dbb.OtelKayits select a; OtelKayit o = new OtelKayit(); foreach (var item in query) { o = item; heap.Insert(o); } HeapSort heapSort = new HeapSort(heap.ReturnHeap()); //heapler icinde en yuksek otel puanına gore asağıdaki sorted ın içine çeker dizi şeklinde HeapDugumu[] sorted = heapSort.Sort(); foreach (var item in sorted) { if (item == null) { break; } if (item.otelKayit.Sehir == metroComboBox2.SelectedItem.ToString() || item.otelKayit.Ilce == metroComboBox3.SelectedItem.ToString()) { lw_otelListesi.Items.Add(item.otelKayit.OtelAd.ToString() + " " + item.otelKayit.OtelPuani.ToString()); } } } }
public void HeapSort() { var algorithm = new HeapSort(); var sortedVector = algorithm.Sort(DataDefinition.UnsortedVector()); Assert.Equal(DataDefinition.SortedVector, sortedVector); }
public void HeapSortTest() { var sort = new HeapSort(); //Test1(sort); RunAllTest(sort); }
private void btnHeapSort_Click(object sender, EventArgs e) //HEAPSORT BUTTON { //Declaring a new instance of HeapSort and displaying properties to the Windows Form HeapSort heapSort = new HeapSort(); richTextBox2.AppendText(heapSort.TimeComplexity + "\n"); richTextBox2.AppendText(heapSort.SpaceComplexity); richTextBox2.AppendText("\n*All Big-O values represent worst-case scenarios unless otherwise noted"); //Displaying the Advantages and Disadvantages of HeapSort to the Windows Form StringBuilder qs = new StringBuilder(); qs.Append("HEAP SORT:\nA Heap sort algorithm works by first organsizing the data to be sorted into a special " + "type of binary tree by setting the largest element of an array in the top of the binary tree, so the heap " + "sort algorithm is very capable of reversing its order at any time.\n\nAdvantages of Heap Sort:\n " + "-The Heap sort algorithm is widely used because of its efficiency.\n -The Heap sort algorithm can be implemented " + "as an in-place sorting algorithm\n -Its memory usage is minimal" + "\n\nDisadvantages of Heap Sort:\n -Heap sort requires more space for sorting\n -Quick sort is much more efficient than Heap in many cases"); richTextBox1.AppendText(qs.ToString()); //Displaying extra notes from a rich text file in the bin using (StreamReader Reader = new StreamReader(@"C:\Users\Clayt\source\repos\CSC205\CSC205_StudyProject\CSC205_StudyProject\bin\HeapSort.rtf")) { while (!Reader.EndOfStream) { richTextBox5.AppendText(Reader.ReadLine()); } } }
private static ISortingStrategy GetSortingStrategy(ObjectTypeEnum objectsType) { ISortingStrategy sortingStrategy = null; switch (objectsType) { case ObjectTypeEnum.Resident: sortingStrategy = new QuickSort(); break; case ObjectTypeEnum.TicketNumber: sortingStrategy = new MergeSort(); break; case ObjectTypeEnum.Passenger: sortingStrategy = new HeapSort(); break; default: break; } System.Console.WriteLine($"Type of elements in collection: {objectsType} -> Chosen Strategy: {sortingStrategy}"); return(sortingStrategy); }
public void InsertionSort_Test() { // Arrange var collectionInsertion = new Collection <int>(); var collectionMerge = new Collection <int>(); var collectionHeap = new Collection <int>(); var collectionRadix = new Collection <int>(); Random random = new Random(); // Act for (int i = 0; i < 1000; i++) { var number = random.Next(0, 10000); collectionInsertion.Add(number); collectionMerge.Add(number); collectionHeap.Add(number); collectionRadix.Add(number); } InsertionSort <int> .Sort(collectionInsertion); collectionMerge = MergeSort <int> .Sort(collectionMerge); HeapSort <int> .Sort(collectionHeap); RadixSort.Sort(collectionRadix); // Assert for (int i = 0; i < collectionInsertion.Count; i++) { Assert.AreEqual(collectionInsertion[i], collectionMerge[i]); Assert.AreEqual(collectionHeap[i], collectionMerge[i]); Assert.AreEqual(collectionRadix[i], collectionHeap[i]); } }
public void millionTest() { HeapSort <int> hs = new HeapSort <int>(); int[] arr = new int[1000000]; int[] done = new int[1000000]; for (int i = 0; i < 1000000; i++) { done[i] = i; arr[999999 - i] = i; } Random r = new Random(); int x, y, z; for (int i = 0; i < 1000000; i++) { x = r.Next(1000000); y = r.Next(1000000); z = arr[x]; arr[x] = arr[y]; arr[y] = z; } hs.Sort(arr, new mycomparer()); for (int i = 0; i < arr.Length; i++) { Assert.AreEqual(arr[i], done[i]); } }
public void SetUp() { _bubbleSort = new BubbleSort(); _quickSort = new QuickSort(); _heapSort = new HeapSort(); _mergeSort = new MergeSort(); }
public void HeapSort_GetNextMax_4_One_More() { HeapSort heap = new HeapSort(new int[] { 5, 8, 6, 3 }); Console.WriteLine(heap.HeapObject.HeapSize); int[] expectedArray = { 0, 3, 5, 6, 8 }; for (int i = 4; i >= 0; i--) { Array.ForEach(heap.HeapObject.HeapArray, item => Console.Write(item + " ")); Assert.AreEqual(i, heap.HeapObject.HeapSize); Assert.AreEqual(expectedArray[i], heap.HeapObject.HeapArray[0]); int maxKey = heap.GetNextMax(); if (i != 0) { Assert.AreEqual(expectedArray[i], maxKey); } else { Assert.AreEqual(-1, maxKey); } Console.WriteLine("Max = " + maxKey); } }
public void Sort_HumansList_SortedHumansList_BySecondName() { //arrange HeapSort heapSort = new HeapSort(); List <Human> expected = new List <Human>() { new Human("Amin", "Avarin", 2000), new Human("Igor", "Baranov", 1990), new Human("Ivan", "Ivanov", 2000), new Human("Vlad", "Klemenko", 2001), new Human("Yan", "Koght", 1998), new Human("Petr", "Kuleshov", 1999), new Human("Ivan", "Kuleshov", 2010), new Human("Alesha", "Popov", 2003), }; //act List <Human> actual = new List <Human>() { new Human("Alesha", "Popov", 2003), new Human("Amin", "Avarin", 2000), new Human("Igor", "Baranov", 1990), new Human("Ivan", "Kuleshov", 2010), new Human("Ivan", "Ivanov", 2000), new Human("Petr", "Kuleshov", 1999), new Human("Vlad", "Klemenko", 2001), new Human("Yan", "Koght", 1998), }; heapSort.Sort(actual, Program.CompareBySecondName); //assert CollectionAssert.AreEqual(expected, actual); }
public void HeapSort_GetNextMax_4() { HeapSort heap = new HeapSort(new int[] { 1, 4, 2, 3 }); Console.WriteLine(heap.HeapObject.HeapSize); for (int i = 4; i >= 0; i--) { Array.ForEach(heap.HeapObject.HeapArray, item => Console.Write(item + " ")); Assert.AreEqual(i, heap.HeapObject.HeapSize); Assert.AreEqual(i, heap.HeapObject.HeapArray[0]); int maxKey = heap.GetNextMax(); if (i != 0) { Assert.AreEqual(i, maxKey); } else { Assert.AreEqual(-1, maxKey); } Console.WriteLine("Max = " + maxKey); } }
/// <summary> /// We will sort the array /// Have a startPointer and endPointer. /// if(arr[startPointer]+arr[endPointer])>sum, decrement endPointer /// if(arr[startPointer]+arr[endPointer])<sum, increment startPointer /// if(arr[startPointer]+arr[endPointer])==sum, add this in list and decrement endPtr and increment startPtr /// /// Running time = O(nlogn) for sorting /// Space = O(1) /// </summary> /// <param name="arr">array from which the sum needs to be calculated</param> /// <param name="sum">sum value</param> /// <returns></returns> public static List <ArrayPair> GetIndiciesWhenSumIsFoundAlgo1(int[] arr, int sum) { List <ArrayPair> ret = new List <ArrayPair>(); // Sort the array HeapSort <int> hs = new HeapSort <int>(arr); arr = hs.HeapArray; int startPointer = 0; int endPointer = arr.Length - 1; while (startPointer < endPointer) { if (arr[startPointer] + arr[endPointer] > sum) { endPointer--; } else if (arr[startPointer] + arr[endPointer] < sum) { startPointer++; } else { //arr[startPointer]+arr[endPointer]==sum ret.Add(new ArrayPair(arr[startPointer], arr[endPointer])); startPointer++; endPointer--; } } return(ret); }
public void SimpleTest() { var arr = new[] { 73, 54, 14, 94, 41, 12, 56, 21, 44 }; HeapSort.Sort(arr); Assert.IsTrue(new[] { 12, 14, 21, 41, 44, 54, 56, 73, 94 }.AllElementsEqualUnchecked(arr)); }
//This will show how the num list runs on each algorithm //This allows the user to compare them public void hitSpeedCompButton() { algView = !algView; //Change whether you are looking at the algorithm or speed comparison if (algView) //If you are now viewing at the algorithm pseudocode { algText.text = oldAlgText; //Change the text back to the pseudocode if (!half) { algDescription.enabled = true; } speedCompText.text = "Speed Comparison"; } else//Otherwise display the speed comparison { oldAlgText = algText.text; string storageText = "Bubble Sort:{0} μs\nCocktail Sort: {1} μs\nComb Sort: {2} μs\nHeap Sort: {3}" + " μs\nInsertion Sort: {4} μs\nMerge Sort:{5} μs\nQuick Sort:{6} μs\nShell Sort:{7} μs\nFlash Sort:{8} μs" + "\nBucket Sort:{9} μs\nRadix Sort:{10} μs"; //Format the text to show each element in microseconds algText.text = string.Format(storageText, BubbleSort.BubbleSortTime(new List <int>(copy)), CocktailSort.CocktailSortTime(new List <int>(copy)), CombSort.CombSortTime(new List <int>(copy)), HeapSort.HeapSortStartTime(new List <int>(copy)), InsertionSort.InsertionSortTime(new List <int>(copy)), Mergesort.MergeSortTime(new List <int>(copy), 0, copy.Count - 1), QuickSort.QuickSortStartTime(new List <int>(copy), 0, copy.Count - 1), ShellSort.ShellSortTime(new List <int>(copy)), FlashSort.FlashSortTime(new List <int>(copy)), BucketSort.BucketSortTime(new List <int>(copy), 34) , RadixSort.RadixSortTime(new List <int>(copy))); algDescription.enabled = false; speedCompText.text = "Algorithm"; } }
public void HeapSortSingletonArray() { int[] a = new int[] { 88 }; int[] expectedOutput = a.Clone() as int[]; HeapSort.Sort(a); Assert.IsTrue(ArrayUtility.AreIntegerEnumerablesEqual(a, expectedOutput)); }
public void HeapSortTest() { HeapSort sort = new HeapSort(); var result = sort.Sort(new int[] { 6, 5, 4, 3, 2, 1 }); Console.WriteLine(Commons.ListPrinter("Heap Sort", result.ToList())); }
private static void TrySort() { SortBase sort = new InsertSort(); sort.Print("-------- before insert sort ----------"); sort.DoSort(); sort.Print("-------- after insert sort ----------"); SortBase ms = new MergeSort(); ms.Print("-------- before merge sort ----------"); ms.DoSort(); ms.Print("-------- after merge sort ----------"); SortBase hs = new HeapSort(); hs.Print("-------- before heap sort ----------"); hs.DoSort(); hs.Print("-------- after heap sort ----------"); QuickSort qs = new QuickSort(); qs.Print("-------- before quick sort ----------"); qs.DoSort(); qs.Print("-------- after quick sort ----------"); CountingSort cs = new CountingSort(); cs.Print("-------- before quick sort ----------"); cs.DoSort(); cs.Print("-------- after quick sort ----------"); }
public void HeapSortTest() { var arr = TestData.NewUnsortedArray; var sorter = new HeapSort(); TestHelper.DoTests(arr, sorter); }
public void TestHeapSort() { var sort = new HeapSort(); sort.Sort(_arr); CollectionAssert.AreEqual(_arr, _expectedArr); }
/// <summary> /// sort the current table /// </summary> /// <param name="sortKeyId">the id of the key to sort by </param> /// <param name="sortDir"></param> protected internal void SortTable(int sortKeyId, char sortDir) { if (!_isLoaded) { Load(); } //if the table is already sorted the way we want it if (sortKeyId != _currSortKey || sortDir != _currSortDir) { _currSortDir = sortDir; _currSortKey = sortKeyId; var recordsArr = new Record[_records.Count]; _records.CopyTo(recordsArr); HeapSort.sort(recordsArr); if (_currSortDir == 'D') { ReverseVector(recordsArr); } for (int i = 0; i < _records.Count; i++) { _records[i] = recordsArr[i]; } } }
public void Sort_WithReverselySortedDistinctValues() { var values = new List <int>(Constants.ArrayWithReverselySortedDistinctValues); values = HeapSort.Sort(values); Assert.IsTrue(UtilsTests.IsSortedAscendingly(values)); }
static void ConstantTableTest() { //tworzenie tablicy bazowej - wypełninonej zerami int[] _oBaseTable = new int[g_iBaseTabSize]; //tworzenie obiektów sortujących SelectionSort selectionSort = new SelectionSort(_oBaseTable); InsertionSort insertionSort = new InsertionSort(_oBaseTable); CocktailSort cocktailSort = new CocktailSort(_oBaseTable); HeapSort heapSort = new HeapSort(_oBaseTable); //pętla po typach algoryutmów sortujących for (int type = 0; type < 4; type++) { //wypisanie informacji dla każdego typu algorytmu switch (type) { case 0: Console.WriteLine("SelectionSort"); break; case 1: Console.WriteLine("InsertionSort"); break; case 2: Console.WriteLine("CocktailSort"); break; case 3: Console.WriteLine("HeapSort"); break; } //główna pętla wielkości sortowanych tablic for (int i = 50_000; i <= g_iBaseTabSize; i += 5_000) { //określa który algorytm ma sortować switch (type) { case 0: selectionSort.MeasureSortTime(i); break; case 1: insertionSort.MeasureSortTime(i); break; case 2: cocktailSort.MeasureSortTime(i); break; case 3: heapSort.MeasureSortTime(i); break; } } } }
public void IsEqualCollections(int[] received, int[] expected) { var heapSort = new HeapSort(); heapSort.Sort(received); Array.Sort(expected); CollectionAssert.AreEqual(received, expected); }
public void HeapSort() { var input = "\0SORTEXAMPLE".ToCharArray(); var sort = new HeapSort <char>(); sort.Go(input); Assert.AreEqual("\0AEELMOPRSTX", new string(input)); }
static void Main(string[] args) { int[] arr = { 10, 64, 7, 52, 32, 18, 2, 48 }; HeapSort hs = new HeapSort(); hs.PerformHeapSort(arr); Console.ReadLine(); }
public void FirstTry_Basic_Test() { int[] listOfNumbers = new int[] { 10, 6, 7, 5, 15, 17, 12 }; var sortedArray = new HeapSort().FirstTry(listOfNumbers); Assert.Equal(new int[] { 5, 6, 7, 10, 12, 15, 17 }, sortedArray); }
public void HeapSort_SortsIntsArray() { var elements = new char[] { 'S', 'O', 'R', 'T', 'E', 'X', 'A', 'M', 'P', 'L', 'E' }; HeapSort <char> .Sort(elements); Assert.Equal(new char[] { 'A', 'E', 'E', 'L', 'M', 'O', 'P', 'R', 'S', 'T', 'X' }, elements); }
public void Sort_WithDistinctValues() { // SortTests.TestSortMethodWithDifferentInputs(HeapSort.Sort); var values = new List <int>(Constants.ArrayWithDistinctValues); values = HeapSort.Sort(values); Assert.IsTrue(UtilsTests.IsSortedAscendingly(values)); }
public void Sort1() { var toSort = new int[] { 1, 2, 3, 4, 5, 6 }; ISort sort = new HeapSort(); var sorted = sort.Sort(toSort); CollectionAssert.AreEqual(sorted, new int[] { 1, 2, 3, 4, 5, 6 }); }
public void BasicSort() { var gen = new Random(15); foreach (var N in new[] { 1, 3, 4, 5 }) { var heapSort = new HeapSort<int>(); var array1 = Enumerable.Range(0, N).Select(i => gen.Next(int.MaxValue)).ToArray(); heapSort.Sort(array1); Assert.IsTrue(array1.IsSorted(), "#B01 " + N); } }
private static void TestHeapSort() { int[] a = {0, 1, 4, 6, 8, 10, 2, 5, 3, 0, 7 }; HeapSort<int> mh = new HeapSort<int>(); System.Console.WriteLine("HeapSort Ascending"); mh.Sort(a, SortDirection.Ascending); DisplayArray(a); DisplaySeparator(); System.Console.WriteLine("HeapSort Descending"); mh.Sort(a, SortDirection.Descending); DisplayArray(a); DisplaySeparator(); }
public void Initialize(int size, double minX, double maxX, double minY, double maxY) { if (size <= 0) throw new ArgumentOutOfRangeException(); geometrypoints = new GeometryPoint[size]; Random rnd = new Random(DateTime.Now.Millisecond); double xRange = maxX - minX; double yRange = maxY - minY; int MaxXPointIndex = 0;//选取x坐标最大的点 for (int i = 0; i < size; i++) { GeometryPoint gp = new GeometryPoint(minX + xRange * rnd.NextDouble(), minY + yRange * rnd.NextDouble()); geometrypoints[i] = gp; if (geometrypoints[MaxXPointIndex].X < gp.X)////选取x坐标最大的点 { MaxXPointIndex = i; } else if (geometrypoints[MaxXPointIndex].X < gp.X && geometrypoints[MaxXPointIndex].Y > gp.Y)//选取x坐标最大的点,如果最大x坐标点有多个,去y最小者 { MaxXPointIndex = i; } } //计算斜率 for (int i = 0; i < size; i++) { if (i == MaxXPointIndex) { geometrypoints[MaxXPointIndex].SLOPE = double.MaxValue; } else { if (geometrypoints[i].X == geometrypoints[MaxXPointIndex].X)//与最大x坐标的x相同的点,因为x坐标之差为零,所以取SLOPE最大值 { geometrypoints[i].SLOPE = double.MaxValue; } else//计算斜率,注意正切函数在-0.5Pi和0.5Pi之间是单调递增的 { geometrypoints[i].SLOPE = (geometrypoints[i].Y - geometrypoints[MaxXPointIndex].Y) / (geometrypoints[MaxXPointIndex].X - geometrypoints[i].X); } } } //按照斜率slope排序,取稳定排序方法的堆排序。 HeapSort<GeometryPoint> heapsort = new HeapSort<GeometryPoint>(); heapsort.Sort(this.geometrypoints,0,size-1); }
public static ISortingStrategy GetSortingStrategy(ObjectToSort objectToSort) { ISortingStrategy sortingStrategy = null; switch (objectToSort) { case ObjectToSort.StudentNumber: sortingStrategy = new MergeSort(); break; case ObjectToSort.RailwayPassangers: sortingStrategy = new HeapSort(); break; case ObjectToSort.CountryResidents: sortingStrategy = new QuickSort(); break; default: throw new ArgumentOutOfRangeException("Unsupported object to sort"); } return sortingStrategy; }
public static void HeapSort(int[] a, SortDirection direction) { HeapSort<int> hs = new HeapSort<int>(); hs.Sort(a, direction); }
public void Initialize() { sorter = new HeapSort<int>(); }
static void Main(string[] args) { int[] arr = new int[10000]; Random rnd = new Random(); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } ISort<int> s = new BubbleSort<int>(); DateTime dt = DateTime.Now; arr = s.Sorting(arr); Console.WriteLine("Time for BubbleSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new CocktailSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for CocktailSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new EvenOddSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for EvenOddSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new CombSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for CombSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new GnomeSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for GnomeSort is {0}.", DateTime.Now - dt); arr = new int[10000]; for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new InsertionSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for InsertionSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new BinaryInsertionSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for BinaryInsertionSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new ShellSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for ShellSort is {0}.", DateTime.Now - dt); arr = new int[1000000]; for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(1000000); } for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(10000); } dt = DateTime.Now; s = new HeapSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for HeapSort is {0}.", DateTime.Now - dt); int ddd = 0; for (int i = 0; i < arr.Length - 2; i++) { //Console.Write(arr[i] + " "); if (arr[i] > arr[i + 1]) //Console.WriteLine("Fatal ERROR!!!"); ddd++; } Console.WriteLine("Error count: {0}", ddd); dt = DateTime.Now; s = new MergeSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for MergeSort is {0}.", DateTime.Now - dt); //StreamWriter sw = new StreamWriter("C:/Users/suvorovi/1.txt"); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(1000000); //sw.Write(arr[i] + " "); } //sw.WriteLine(""); dt = DateTime.Now; s = new QuickSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for QuickSort is {0}.", DateTime.Now - dt); for (int i = 0; i < arr.Length; i++) { arr[i] = rnd.Next(1000000); //sw.Write(arr[i] + " "); } //sw.WriteLine(""); dt = DateTime.Now; s = new TimSort<int>(); arr = s.Sorting(arr); Console.WriteLine("Time for TimSort is {0}.", DateTime.Now - dt); ddd = 0; for (int i = 0; i < arr.Length - 2; i++) { //Console.Write(arr[i] + " "); if (arr[i] > arr[i + 1]) //Console.WriteLine("Fatal ERROR!!!"); ddd++; } Console.WriteLine("Error count: {0}", ddd); Console.ReadLine(); //sw.Close(); }