public int KthSmallestElementIn2DArray(int[,] arr, int k) { MinHeap minHeap; minHeap = new MinHeap(arr.GetLength(0)); for (int index = 0; index < arr.GetLength(0); index++) { minHeap.insertKey(arr[0, index]); } int count = 1; for (int column = 0; column < arr.GetLength(0) && count < k; column++) { for (int row = 1; row < arr.GetLength(1) && count < k; row++) { minHeap.heapArr[0] = arr[row, column]; count++; minHeap.MinHeapify(0); } } return(minHeap.heapArr[0]); }
public void MinHeapify() { int[] inputArr = { 7, 10, 4, 3, 20, 15 }; int[] expectedArr = { 3, 4, 7, 10, 20, 15 }; minHeap = new MinHeap(inputArr.Length); for (int i = 0; i < inputArr.Length; i++) { minHeap.insertKey(inputArr[i]); } minHeap.MinHeapify(0); Assert.True(ArrayTestUtils.ArraysEqual(minHeap.heapArr, expectedArr)); }