Exemplo n.º 1
0
int ArraySorter::DeleteMax(int *arr, int end)
{
	int max = arr[0];
	arr[0] = arr[end];
	PercolateDown(arr, 0, end-1);
	return max;
}
Exemplo n.º 2
0
/* 建堆方法,只需线性时间建好;
   建堆的结果:数组的第一个元素(即树根)是所有元素中的最小值,索引小于等于size/2-1的其它元素(即其它非叶子节点)的值都是其所在子树的最小值 */  
void BuildHeap(int num[] ,int size) {
    int i;
    //从最后一个非叶子节点开始,对每个非叶子节点进型最小根调整,保证每个根节点都是其子树中的最小值
    for (i = size / 2 - 1; i >= 0; i--) {  
        PercolateDown(num, i,size);// 进行下滤操作
        PrintHeap("Build heap:",num,size);
    }  
}
Exemplo n.º 3
0
//it functions as delete min
int DeleteMax(Heap* heap){
if(heap->count>0){
int tmpData=heap->Array[0];
heap->Array[0]=heap->Array[heap->count-1];
heap->count--;
PercolateDown(heap,0);
return tmpData;
}else{
return -1;
}
}
void HeapSort(int *a, int n, int* Unfinished)
{
	for (int i = n / 2; i >= 0; i--){
		PercolateDown(a, i, n);
        /*
        if(isSame(a, Unfinished, n)){
            printf("Heap Sort\n");
            if(i >= 1){ 
                i--;
                PercolateDown(a, i, n);
                print(a, n);
                exit(0);
            }
            else{
                i = n - 1;
                int temp = a[0];
                a[0] = a[i];
                a[i] = temp;
                PercolateDown(a, 0, i);
            }
        }
        */
    }
	for (int i = n - 1; i > 0 ; i--){
		int temp = a[0];
		a[0] = a[i];
		a[i] = temp;
		PercolateDown(a, 0, i);
        if(isSame(a, Unfinished, n)){
            printf("Heap Sort\n");
            i--;
            int temp = a[0];
            a[0] = a[i];
            a[i] = temp;
            PercolateDown(a, 0, i);
            print(a, n);
        }
	}
}
int PriorityQueue::DeleteTop()
{
   if(count != 0)
   {
     int data = A[0];
     A[0] = A[count - 1];
     A[count - 1] = 0;
     count--;
     PercolateDown(0);
     return data;
   }
   return -1;
}
Exemplo n.º 6
0
void GetMinK(Heap* heap,int* Array,int n,int k){

int i,tmpData;
for(i=k;i<n;i++){
    tmpData=GetMax(heap);
    if(tmpData>Array[i]){
    heap->Array[0]=Array[i];
    PercolateDown(heap,0);
    }
}


}
Exemplo n.º 7
0
void HeapSort(int num[] ,int size)
{
    int i;
    int iLength=size;
     
    PrintHeap("Befor Sort:",num,iLength);
     
    BuildHeap(num,size);// 建立小顶堆  
     
    for (i = iLength - 1; i >= 1; i--) {  
        Swap(num, 0, i);// 交换  
        size--;// 每交换一次让规模减少一次  
        PercolateDown(num, 0,size);// 将新的首元素下滤操作
        PrintHeap("Sort Heap:",num,iLength); 
    }
}
Exemplo n.º 8
0
void PercolateDown(HNode* heapRoot,int i){

int left=Left(heapRoot,i);
int right=Right(heapRoot,i);
int min;
//printf("\nHere");
if(left!=-1 && heapRoot->HArray[left]->data< heapRoot->HArray[i]->data){
min=left;
}else{
min=i;
}

if(right!=-1 && heapRoot->HArray[right]->data<heapRoot->HArray[min]->data){
min=right;
}
//struct HeapNode* tmpNode=(struct HeapNode*)malloc(sizeof(struct HeapNode));
if(min!=i){
/*
tmpNode->data=heapRoot->HArray[i]->data;
tmpNode->arrayIndex=heapRoot->HArray[i]->arrayIndex;
tmpNode->elmIndex=heapRoot->HArray[i]->elmIndex;
heapRoot->HArray[i]->data=heapRoot->HArray[min]->data;
heapRoot->HArray[i]->arrayIndex=heapRoot->HArray[min]->arrayIndex;
heapRoot->HArray[i]->elmIndex=heapRoot->HArray[min]->elmIndex;
heapRoot->HArray[min]->data=tmpNode->data;
heapRoot->HArray[min]->arrayIndex=tmpNode->arrayIndex;
heapRoot->HArray[min]->elmIndex=tmpNode->elmIndex;
*/
int data=heapRoot->HArray[i]->data;
int arrayIndex=heapRoot->HArray[i]->arrayIndex;
int elmIndex=heapRoot->HArray[i]->elmIndex;

heapRoot->HArray[i]->data=heapRoot->HArray[min]->data;
heapRoot->HArray[i]->arrayIndex=heapRoot->HArray[min]->arrayIndex;
heapRoot->HArray[i]->elmIndex=heapRoot->HArray[min]->elmIndex;


heapRoot->HArray[min]->data=data;
heapRoot->HArray[min]->arrayIndex=arrayIndex;
heapRoot->HArray[min]->elmIndex=elmIndex;

PercolateDown(heapRoot,min);
}

}
Exemplo n.º 9
0
void HeapSort(double *pList, int cbLength )
{
	int i;
	double tmp;
    
	for( i = 1; i < cbLength; i++ ) {
		PercolateUp( i, pList );
	}

	for( i = cbLength - 1; i > 0; i-- ) {
		tmp = pList[0];
		pList[0] = pList[i];
		pList[i] = tmp;

		PercolateDown( i - 1, pList );

	}
}
void PriorityQueue::DeleteElement(int data)
{
   int i = 0;
   for(;i<count;i++)
   {
      if(A[i] == data)
	break;
   }
   if(i == count) 
   {
     printf("Element not found in the heap. %d\n",data);
     return;
   }
   A[i] = A[count - 1];
   A[count - 1] = 0;
   count--;
   PercolateDown(i);
}
Exemplo n.º 11
0
void ArraySorter::CreateHeap(int* arr, int n)
{
	// Percolate down for each parent node
	int start = (n-1)/2;
	while(start >= 0)
	{
		PercolateDown(arr, start, n-1);
		start--;
	}

	// Remove max from heap (and place at end of array)
	int end = n-1;
	int max = 0;
	for(; end > 0; end--)
	{
		max = DeleteMax(arr, end);
		arr[end] = max;
	}
}
Exemplo n.º 12
0
PriorityQueue BuildHeap(ElementType *x, int n)
{
    PriorityQueue H = NULL;
    int i = 0;

    H = Initialize(n);      /* include ele[0]=最小值 */
    for(i=0; i<n; i++)
    {
        H->Elements[i+1] = x[i];
    }

    H->size = n;
    for(i=n/2; i>0; i--)
    {
        PercolateDown(H, i);
    }

    return H;
}
Exemplo n.º 13
0
void BuildHeap(Heap* heap,int* Array,int n){

if(!heap){
return;
}

while(heap->capacity<n){
ResizeHeap(heap);
}

int i;
for(i=0;i<n;i++){
heap->Array[i]=Array[i];
}
heap->count=n;
for(i=(n-1)/2;i>=0;i--){
PercolateDown(heap,i);
}

}
Exemplo n.º 14
0
struct HeapNode* ExtractMin(HNode* heapRoot){
struct HeapNode* tmpNode=(struct HeapNode*)malloc(sizeof(struct HeapNode));

tmpNode->data=heapRoot->HArray[0]->data;
tmpNode->arrayIndex=heapRoot->HArray[0]->arrayIndex;
tmpNode->elmIndex=heapRoot->HArray[0]->elmIndex;

//struct HeapNode* tmpNode=heapRoot->HArray[0];

if(heapRoot->count>1){
heapRoot->HArray[0]->data=heapRoot->HArray[heapRoot->count-1]->data;
heapRoot->HArray[0]->arrayIndex=heapRoot->HArray[heapRoot->count-1]->arrayIndex;
heapRoot->HArray[0]->elmIndex=heapRoot->HArray[heapRoot->count-1]->elmIndex;
heapRoot->count--;
PercolateDown(heapRoot,0);
}else{
heapRoot->count--;
}

return tmpNode;

}
Exemplo n.º 15
0
void HeapSort(Heap* heap){
if(!heap){
return;
}
int n=heap->count;
int i;
int tmp;
printf("\n");
for(i=(n-1);i>0;i--){
tmp=heap->Array[0];
//printf("%d  ",tmp);
heap->Array[0]=heap->Array[heap->count-1];
heap->Array[heap->count-1]=tmp;
heap->count--;
PercolateDown(heap,0);
}
heap->count=n;
/*
for(i=0;i<n;i++){
printf("%d  ",heap->Array[i]);
}
*/
}
void PriorityQueue::PercolateDown(int i)
{
   int max = -1;
   int left  = LeftChild(i);
   int right = RightChild(i);
   
   if(left != -1 && heap_type == MAX_HEAP && A[left] > A[i]  ||
      left != -1 && heap_type == MIN_HEAP && A[left] < A[i])
    max = left;
   else
    max = i;
   
   if(right != -1 && heap_type == MAX_HEAP && A[right] > A[max] ||
      right != -1 && heap_type == MIN_HEAP && A[right] < A[max])
    max = right;
    
    if(max != i) //Swap with max index
    {
      int tmp = A[i];
      A[i] = A[max];
      A[max] = tmp;
      PercolateDown(max);
    }
}
Exemplo n.º 17
0
void PercolateDown(Heap* heap,int i){
int left=GetLeftChild(heap,i);
int right=GetRightChild(heap,i);

int min;

if(left !=-1 && heap->Array[i]>heap->Array[left]){
min=left;
}else{
min=i;
}

if(right !=-1 && heap->Array[min]>heap->Array[right]){
min=right;
}

if(min!=i){
int tmpData=heap->Array[min];
heap->Array[min]=heap->Array[i];
heap->Array[i]=tmpData;
PercolateDown(heap,min);
}

}