Exemplo n.º 1
0
void MaxHeapify(int list[], int index, int heapsize)
{

    int parent = list[index];
    int leftchild = list[index*2];
    int rightchild = list[index*2 +1];
    int largest = index; //that means, parent index is set to largest first

    /* Find larger between parent and leftchild */
    if (index*2 <= heapsize && leftchild > list[largest])
        largest = index*2;

    /* Find larger between larger from last operation and right child */
    if ((index*2+1) <= heapsize && rightchild > list[largest])
        largest = index*2 + 1;

    /* Parent is not largest any more that means, left or rightchild is larger */
    if (largest != index) {
        /* Swap parent and whichever is largest */
        int temp = parent;
        if (largest == index*2) {
            list[index] = list[index*2];
            list[index*2] = temp;
            MaxHeapify(list, index*2, heapsize);
        } else {
            list[index] = list[index*2+1];
            list[index*2+1] = temp;
            MaxHeapify(list, index*2+1, heapsize);
        }
    }
}
Exemplo n.º 2
0
void HeapSort::BuildHeap(int* heap, int heapsize)
{
   for(int i=heapsize/2; i>0; i--)
   {
	   MaxHeapify(heap, i, heapsize);
   }
}
Exemplo n.º 3
0
void BuildMaxHeap(int* pBuffer, uint32_t nSize)
{
    for (int i = nSize/2; i >= 0; --i)
    {
        MaxHeapify(pBuffer, i, nSize);
    }
}
Exemplo n.º 4
0
void HeapSort::sort(int* array, int size)
{
	
   m_array= new int[size +1];
   for(int i=0; i<size; i++)
   {
	   m_array[i+1]= array[i];
   }
   BuildHeap(m_array,size);
   for(i=1; i<=10; i++)
   {
	   cout<<m_array[i]<<endl;
   }
   int tmp;
   for(int j=size; j>=1; j--)
   {
	   MaxHeapify(m_array, 1, j);
	   tmp=m_array[1];
	   m_array[1] = m_array[j];
	   m_array[j]=tmp;
   }
   for(int k=1; k<size+1 ; k++)
   {
	   array[k-1]=m_array[k];
   }
   delete[] m_array;

   

};
Exemplo n.º 5
0
/**
 * @function MaxHeapify
 */
void MaxHeapify( std::vector<int> &_A,
	    int _i ) {
  
  int heapSize = _A.size();
  int largest;
  int temp;

  int l = Left(_i);
  int r = Right(_i);

  if( l < heapSize && _A[l] > _A[_i] ) {
    largest = l;
  }
  else {
    largest = _i;
  }
  if( r < heapSize && _A[r] > _A[largest] ) {
    largest = r;
  }

  if( largest != _i ) {

    // Exchange
    temp = _A[_i];
    _A[_i] = _A[largest];
    _A[largest] = temp;

    MaxHeapify( _A, largest );
  }  
}
void BuildMaxHeap(int *pnArr, int nLen)
{
    for (int i = Parent(nLen -1); i >= 0; i--)
    {
        MaxHeapify(pnArr, nLen, i);
    }
}
void MaxHeapify(int *pnArr, int nLen, int i)
{
    int LChild = LeftChild(i);
    int RChild = RightChild(i);
    int nMaxPos;
    if (LChild < nLen && pnArr[LChild] > pnArr[i])
    {
        nMaxPos = LChild;
    }
    else
    {
        nMaxPos = i;
    }
    if (RChild < nLen && pnArr[RChild] > pnArr[nMaxPos])
    {
        nMaxPos = RChild;
    }
    
    if (nMaxPos != i)
    {
        Swap(&pnArr[nMaxPos], &pnArr[i]);
        MaxHeapify(pnArr, nLen,nMaxPos);
    }
    
}
Exemplo n.º 8
0
void HeapSort(int list[], int num)
{
    int k;

    int heapsize = num;

    /* Create Max-Heap */
    BuildMaxHeap(list, heapsize);
    printf("After build heap, lets check...\n");
    for(k=1; k <= num; k++)
        printf("[%d]", arr[k]);
    printf("\n");

    /* Until heapsize is 1, Swap root node of Max heap with last child and call Max-Heap*/

    while(heapsize >1) {
        /*Swap */
        int temp = list[1];
        list[1] = list[heapsize];
        list[heapsize] = temp;
        heapsize--;
        MaxHeapify(list, 1, heapsize);
    }

}
Exemplo n.º 9
0
void HeapSort::MaxHeapify(int* heap, int root_index , int heapsize)
{
	int left_index=2*root_index;
	int right_index=2*root_index+1;
	int largest_index = root_index;
	int tmp;
	if(left_index<=heapsize && heap[root_index]>= heap[left_index] )
	{
        largest_index = root_index;
	}
	else if(left_index<=heapsize && heap[root_index] < heap[left_index] )
	{
		largest_index= left_index;
	}
	 if(right_index<=heapsize && heap[largest_index] < heap[right_index] )
	{
		largest_index= right_index;
	}
	 if(largest_index != root_index)
	 {
           tmp=heap[largest_index];
		   heap[largest_index]= heap[root_index];
		   heap[root_index]= tmp;
		   MaxHeapify(heap, largest_index, heapsize);
	 }
	
}
Exemplo n.º 10
0
int PriorityQueue::Pop()
{
	int ret = Top();
	std::swap(heap_.at(0), heap_.at(heap_.size() - 1));
	heap_.pop_back();
	MaxHeapify(0);
	return ret;
}
Exemplo n.º 11
0
void BuildMaxHeap(int *A, int length)
{
	int heapsize = length;
	for (int i = length / 2; i > 0; i--)
	{
		MaxHeapify(A, i, heapsize);
	}
}
void MaxHeapify(int *H,int n,int k)         // max-heapify function for setting the ordering property of max heap
{
  int max=k;
  if((2*k+1)<n){if(H[max]<H[2*k+1])max=2*k+1;}
  if((2*k+2)<n){if(H[max]<H[2*k+2])max=2*k+2;}
  if(max==k)return;
  else {swap(H[k],H[max]);MaxHeapify(H,n,max);}
}
Exemplo n.º 13
0
void BuildMaxHeap(int list[], int heapsize)
{
    int k;

    for (k= (heapsize/2); k >=1; k--) {
        MaxHeapify(list, k, heapsize);
    }

}
Exemplo n.º 14
0
/**
 * @function BuildMaxHeap
 */
void BuildMaxHeap( std::vector<int> &_A ) {

  int n = _A.size();
  int heapSize = _A.size();

  for( int i = Parent(n-1); i >= 0; i-- ) {
    printf("MaxHeapify for %d \n", i);
    MaxHeapify( _A, i );
  }
}
void HeapSort(int *pnArr, int nLen)
{
    BuildMaxHeap(pnArr, nLen);
    for (int i = nLen - 1; i > 0; i--)
    {
        Swap(&pnArr[i], &pnArr[0]);
        nLen--;
        MaxHeapify(pnArr, nLen, 0);
    }
}
Exemplo n.º 16
0
int ExtractMax(Heap* heap)
{
	int temp = heap->element[1];
	heap->element[1] = heap->element[heap->size];
	heap->element[heap->size] = temp;

	MaxHeapify(heap, 1);

	return temp;
}
Exemplo n.º 17
0
void HeapSort(int* pBuffer, unsigned int nLen)
{
    BuildMaxHeap(pBuffer, nLen);

    for (int i = nLen - 1; i > 0; --i)
    {
        std::swap(pBuffer[i], pBuffer[0]);
        MaxHeapify(pBuffer, 0, i);
    }
}
Exemplo n.º 18
0
void HeapSort(int *array, int length) {
  BuildMaxHeap(array, length);
  int temp;
  int heap_size = length;
  while(heap_size > 1) {
    temp = array[0];
    array[0] = array[heap_size-1];
    array[heap_size-1] = temp;
    MaxHeapify(array, 0, --heap_size);
  }
}
Exemplo n.º 19
0
void insert(heap *h, int val)
{
    assert(h);
    h->data[h->size++] = val;
//#define MINHEAP
#ifdef MINHEAP /*min-heap proprity queue*/
    MinHeapify(h);
#else
    MaxHeapify(h);
#endif
}
Exemplo n.º 20
0
void BuildMaxHeap(Heap* heap)
{
	if (heap == nullptr)
	{
		return;
	}

	for (int i = heap->size / 2; i > 0; --i)
	{
		MaxHeapify(heap, i);
	}
}
Exemplo n.º 21
0
void HeapSort(int *A, int length)
{
	BuildMaxHeap(A, length);
	int heapsize = length;
	for (int i = length; i > 0; i--)
	{
		int temp = A[0];
		A[0] = A[i-1];
		A[i-1] = temp;
		heapsize--;
		MaxHeapify(A, 1, heapsize);
	}
}
Exemplo n.º 22
0
// pre-condition: left-subtree and right-subtree both are max-heap
void PriorityQueue::MaxHeapify(int i)
{
	int lch = 2 * i;
	int rch = 2 * i + 1;
	int max = i;
	if (lch < heap_.size() && heap_.at(lch) > i)
		max = lch;
	if (rch < heap_.size() && heap_.at(rch) > max)
		max = rch;
	if (max != i)
	{
		std::swap(heap_.at(max), heap_.at(i));
		MaxHeapify(max);
	}
}
Exemplo n.º 23
0
void PartialHeapSort(int* pBuffer, uint32_t nLen, uint32_t nPartialSize)
{
    BuildMaxHeap(pBuffer, nLen);

    for (int i = nLen - 1; i > nLen - nPartialSize - 1; --i)
    {
        std::swap(pBuffer[i], pBuffer[0]);
        MaxHeapify(pBuffer, 0, i);
    }

    for (int i = 0; i < nPartialSize + 1; ++i)
    {
        std::swap(pBuffer[i], pBuffer[nLen - 1 - i]);
    }
}
Exemplo n.º 24
0
void HeapSort(int* arr, int size)
{
	Heap* heap = MakeHeap(arr, size);

	BuildMaxHeap(heap);

	for (int i = size; i > 0; --i)
	{
		arr[i - 1] = heap->element[1];

		heap->element[1] = heap->element[heap->size];
		heap->size = heap->size - 1;

		MaxHeapify(heap, 1);
	}
}
Exemplo n.º 25
0
void MaxHeapify(int *array, int i, int length) {
  int l = LEFT(i);
  int r = RIGHT(i);
  int largest = i, temp;

  if(l<length && array[l]>array[largest])
    largest = l;

  if(r<length && array[r]>array[largest])
    largest = r;

  if(largest != i) {
    temp = array[i];
    array[i]=array[largest];
    array[largest] = temp;
    MaxHeapify(array, largest, length);
  }
  return;
}
Exemplo n.º 26
0
void MaxHeapify(int* A, int i, int heapsize)
{
	int l = LEFT(i);
	int r = RIGHT(i);
	int largest = i;
	if (l <= heapsize && A[l-1] > A[i-1])
	{
		largest = l;
	}

	if (r <= heapsize && A[r-1] > A[largest-1])
	{
		largest = r;
	}

	if (largest != i)
	{
		int temp = A[i-1];
		A[i-1] = A[largest -1];
		A[largest -1] = temp;
		MaxHeapify(A, largest, heapsize);
	}
}
Exemplo n.º 27
0
void MaxHeapify(Heap* heap, int pos)
{
	if (heap == nullptr)
	{
		return;
	}

	if (heap->size < pos)
	{
		return;
	}

	int large = pos;

	int left = 2 * pos;
	int right = left + 1;

	if (heap->size >= left && heap->element[left] > heap->element[large])
	{
		large = left;
	}

	if (heap->size >= right && heap->element[right] > heap->element[large])
	{
		large = right;
	}

	if (large != pos)
	{
		int temp = heap->element[pos];
		heap->element[pos] = heap->element[large];
		heap->element[large] = temp;
		
		MaxHeapify(heap, large);
	}
}
void deleteMax(int *H,int& n)               //  deletes the maximum element in the max heap
{
  H[0]=H[n-1];n--;
  MaxHeapify(H,n,0);
}
Exemplo n.º 29
0
void BuildMaxHeap(int *array, int length) {
  for(int i=floor((length-1)/2); i>=0; i--)
    MaxHeapify(array, i, length);
  return;
}
Exemplo n.º 30
0
void PriorityQueue::BuildMaxHeap()
{
	for (int i = heap_.size() / 2 - 1; i >= 0; --i)
		MaxHeapify(i);
}