Beispiel #1
0
        private void Swap(int a, int b)
        {
            //  System.out.println("swappinh" + mH[a] + " and " + mH[b]);
            HeapNode temp = this.minHeap[a];

            this.minHeap[a] = this.minHeap[b];
            this.minHeap[b] = temp;
        }
Beispiel #2
0
        public void BubbleUp()
        {
            var pos    = this.position;
            var parent = (pos - 1) / 2;

            while (pos > 0 && this.minHeap[parent].Data > this.minHeap[pos].Data)
            {
                //swap
                HeapNode node = this.minHeap[parent];
                this.minHeap[parent] = this.minHeap[pos];
                this.minHeap[pos]    = node;
                pos    = parent;
                parent = (pos - 1) / 2;
            }
        }
        public int[] merge(int[,] A, int k, int n)
        {
            int nk = n * k;

            result = new int[nk];
            int count = 0;

            int[] ptrs = new int[k];
            // create index pointer for every list.
            for (int i = 0; i < ptrs.Length; i++)
            {
                ptrs[i] = 0;
            }
            for (int i = 0; i < k; i++)
            {
                if (ptrs[i] < n)
                {
                    this.minHeap.Insert(A[i, ptrs[i]], i); // insert the element into heap
                }
                else
                {
                    this.minHeap.Insert(int.MaxValue, i); // if any of this list burns out, insert +infinity
                }
            }
            //this.minHeap.Print();
            while (count < nk)
            {
                HeapNode h = this.minHeap.ExtractMin();                                     // get the min node from the heap.
                                                                                            //this.minHeap.Print();
                result[count] = h.Data;                                                     // store node data into result array
                ptrs[h.ListNumber]++;                                                       // increase the particular list pointer
                if (ptrs[h.ListNumber] < n)
                {                                                                           // check if list is not burns out
                    this.minHeap.Insert(A[h.ListNumber, ptrs[h.ListNumber]], h.ListNumber); // insert the next element from the list
                }
                else
                {
                    this.minHeap.Insert(int.MaxValue, h.ListNumber); // if any of this list burns out, insert +infinity
                }
                count++;
            }
            return(result);
        }