public void TestBuildHeap() { int[] arr = new int[(int)3e+5]; for (int i = -1; ++i < arr.Length; arr[i] = i + 1) { ; } Randomize(arr); var q = MyLittleArrayHeapPriorityQueue <int> .BuildHeap(arr); for (int i = -1; ++i < arr.Length;) { Assert.AreEqual(q.RemoveMin(), i + 1); } }
/// <summary> /// Use the build heap algorithm to sort the array. /// </summary> static void SortAllBuildHeap(MyStopwatch thestopwatch, int arrsize) { int[] arr = new int[arrsize]; for (int i = 0; i < arr.Length; arr[i] = i + 1, i++) { ; } Randomize(arr); thestopwatch.Tick(); IPriorityQ <int> q = MyLittleArrayHeapPriorityQueue <int> .BuildHeap(arr); for (int i = 0; i < arr.Length; i++) { int themin = q.RemoveMin(); if (themin != i + 1) { throw new Exception($"Expected: {i + 1} but {themin}"); } } thestopwatch.Tock(); }