/// <summary> /// Sort and time the time elapse using my Arrayheap. /// </summary> /// <param name="thestopwatch"></param> /// <param name="arrsize"></param> static void SortAll(MyStopwatch thestopwatch, int arrsize) { IPriorityQ <int> q = new MyLittleArrayHeapPriorityQueue <int>(); int[] arr = new int[arrsize]; for (int i = 0; i < arr.Length; arr[i] = i + 1, i++) { ; } Randomize(arr); thestopwatch.Tick(); for (int i = 0; i < arr.Length; q.Enqueue(arr[i]), i++) { ; } 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(); }
/// <summary> /// Sort and time the process using a system sorted set. /// </summary> /// <param name="sw"></param> /// <param name="arrsize"></param> static void SortAllUsingSet(MyStopwatch sw, int arrsize) { SortedSet <int> q = new SortedSet <int>(); int[] arr = new int[arrsize]; for (int i = 0; i < arr.Length; arr[i] = i + 1, i++) { ; } Randomize(arr); sw.Tick(); for (int i = 0; i < arr.Length; q.Add(arr[i]), i++) { ; } for (int i = 0; i < arr.Length; i++) { int themin = q.Min; q.Remove(themin); if (themin != i + 1) { throw new Exception($"Expected: {i + 1} but {themin}"); } } sw.Tock(); }
/// <summary> /// /// </summary> /// <param name="len"> /// The len of the randomized double array. /// </param> /// <param name="k"> /// The number of top K element you want from the list. /// </param> /// <param name="msw"></param> /// <param name="UseMyHeap"></param> public static void TimedTopKSort (int len, int k, MyStopwatch msw, bool UseMyHeap = true) { if (k <= 0 || k > len) { throw new Exception("What in hey is this? "); } double[] RandomArr = RandomDoubleArray(len); msw.Tick(); double[] sorted = UseMyHeap ? TopKSortDoubleArrayUsingBinaryHeap(RandomArr, k): TopKSortDoubleArrayUsingSortedSet(RandomArr, k); msw.Tock(); // Quick Varify for (int i = 1; i < sorted.Length; i++) { Assert.IsTrue(sorted[i] >= sorted[i - 1]); } }