public static int KthLargestItem(int[] array, int k) { if (k < 1 || k > array.Length) { throw new ArgumentOutOfRangeException(); } var heap = new Heaps(7); for (var i = 0; i < array.Length; i++) { heap.Insert(array[i]); } for (var i = 0; i < k - 1; i++) { heap.Remove(); } return(heap.Max()); }
public static void PerformHeapOps() { var heap = new Heaps.Heaps(10); heap.Insert(15); heap.Insert(10); heap.Insert(3); heap.Insert(8); heap.Insert(12); heap.Insert(9); heap.Insert(4); heap.Insert(1); heap.Insert(24); heap.Remove(); var array = new int[5] { 10, 20, 30, 40, 50 }; var heapArray = MaxHeap.Heapify(array); Console.WriteLine(MaxHeap.KthLargestItem(array, 2)); }