//logic: //The matrix is sorted in both rows and column //so the first element will be the smallest. //build min heap from the first row.. //heap node should have data, rowno, colno //get the min element in heap.. //get the col no and get the nextrow on the same and update into the heap (we can do insert if we pop on previou step ) but update and heapify is better for this //get the next smallest //http://www.geeksforgeeks.org/kth-smallest-element-in-a-row-wise-and-column-wise-sorted-2d-array-set-1/ public int FindthekthSmalleseElementInMatrix(int[,] matrix, int k) { MinHeap <MatrixElement> myheap = new MinHeap <MatrixElement>(new MinMatrixElementComparer()); //add first row to minheap//all col in first row for (int col = 0; col < matrix.GetLength(1); col++) { myheap.Insert(new MatrixElement(matrix[0, col], 0, col)); } // to find kth smallest..iterate k times for (int i = 1; i < k; i++) { //get the min MatrixElement minelement = myheap.Peek(); //Find the next element in the next row of the same colmumn //check the next row is not ourside bounds if (minelement.Row + 1 <= matrix.GetLength(1) - 1) { MatrixElement nextelement = new MatrixElement(matrix[minelement.Row + 1, minelement.Col], minelement.Row + 1, minelement.Col); //we can update the heap root element and then call minheapify of the root //my heap fon't prov that so myheap.PopRoot(); myheap.Insert(nextelement); //this will make sure to have the smallest } else { myheap.PopRoot(); } } return(myheap.Peek().Data); }
private void button5_Click(object sender, EventArgs e) { int[] input = AlgorithmHelper.ConvertCommaSeparetedStringToInt(this.textBox4.Text); // arrange // Heap<int> minheap = new Heap<int>(new List<int>(), 0, new KarthicMinHeapComparer()); MinHeap <int> minheap = new MinHeap <int>(new KarthicMinHeapComparer2()); foreach (int i in input) { minheap.Insert(i); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < input.Length; i++) { sb.Append(minheap.PopRoot().ToString()).Append(","); } this.textBox3.Text = sb.ToString(); }
public void InsertRandomElement(int randomvalue, MaxHeap <int> maxheap, MinHeap <int> minheap) { if (maxheap.Size() == minheap.Size()) { if (minheap.Size() > 0 && randomvalue > minheap.Peek()) { //if the coming random value is greater than the minheap peek maxheap.Insert(minheap.PopRoot()); minheap.Insert(randomvalue); } else { maxheap.Insert(randomvalue); } } else { //if the Size() is not equal means the max heap will be bigger if (randomvalue < maxheap.Peek()) { minheap.Insert(maxheap.PopRoot()); maxheap.Insert(randomvalue); } else { minheap.Insert(randomvalue); } } }
private Range FindShortestRange(List <Queue <int> > queuelist) { MinHeap <HeapNodeCustom> heap = new MinHeap <HeapNodeCustom>(new KarthicMinHeapComparer4()); int bestRangemin = Int32.MaxValue; int bestRangemax = Int32.MinValue; //insert head of list into heap for (int i = 0; i < queuelist.Count; i++) { Queue <int> queue = queuelist[i]; heap.Insert(new HeapNodeCustom(queue.Peek(), i)); if (queue.Peek() > bestRangemax) { bestRangemax = queue.Peek(); } } bestRangemin = heap.Peek().Value; Range shortestrange = new Range(bestRangemin, bestRangemax); //initial best to first //we have max value...get the min value from the heap while (true) { HeapNodeCustom minrecord = heap.PopRoot(); //pop the min value //get the list of poped element Queue <int> queue = queuelist[minrecord.IndexofList]; //remove the first element queue.Dequeue(); if (queue.Count == 0) { break; } //insert next element in this queue to the heap heap.Insert(new HeapNodeCustom(queue.Peek(), minrecord.IndexofList)); //we have running value of max if (queue.Peek() > bestRangemax) { bestRangemax = queue.Peek(); } bestRangemin = heap.Peek().Value; Range range = new Range(bestRangemin, bestRangemax); if (range.GetLength() < shortestrange.GetLength()) { shortestrange = range; } } return(shortestrange); }
private void button6_Click(object sender, EventArgs e) { string[] input = this.textBox8.Text.Split(','); MinHeap <KarthicStringComparer> minheap = new MinHeap <KarthicStringComparer>(new KarthicStringComparer()); foreach (string s in input) { minheap.Insert(new KarthicStringComparer(s)); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < input.Length; i++) { sb.Append(minheap.PopRoot().StringOfX).Append(","); } this.textBox7.Text = sb.ToString(); }