void BUILD_MAX_HEAP (heap *A)
{
    A->heap_size = A->length;
    int i;
    for(i = (int)((A->length/2)-1); i>=0; i--)
        MAX_HEAPIFY(A,i);
}
Exemplo n.º 2
0
void BUILD_MAX_HEAO(int a[], int len)
{
	for (int i = len / 2; i > -1; --i)
	{
		MAX_HEAPIFY(a, i, len);                                        //从下往上,保证每个都是最大堆
	}
}
Exemplo n.º 3
0
void BUILD_MAX_HEAP(DATA_TYPE *A,int len) {    
    HEAP_SIZE = len;    
    int i;    
    for (i=(int)(len/2);i>=1;i--) {        
        MAX_HEAPIFY(A,i);    
    }
}
Exemplo n.º 4
0
void MAX_HEAPIFY(int* A,int len, int i)
{
	int left_child=2*i+1;//下标i的左孩子 
	int right_child=2*i+2;//下标i的右孩子
	
	int xiabiao=i;
	int data=-1;
	
	if(left_child<len && A[left_child]>A[i])
	{
		data=A[left_child];
		xiabiao=left_child;
	}
	else
	{
		data=A[i];
		xiabiao=i;
	}
	if(right_child<len && A[right_child]>A[xiabiao])
	{
		data=A[right_child];
		xiabiao=right_child;
	}
	
	if(xiabiao!=i)
	{
		A[xiabiao]=A[i];
		A[i]=data;
		MAX_HEAPIFY(A,len,xiabiao);
	}
}
Exemplo n.º 5
0
void BUILD_MAX_HEAP(int* A,int len)//建堆 
{
	int n=len; //堆排序中,运用了满二叉树的思想,所以len/2+1这部分都是叶子节点,不用考虑 
	//二叉树 从下向上建立最大堆
	for(int i=n-1;i>=0;i--) 
	{
		MAX_HEAPIFY(A,len,i);
	}
}
Exemplo n.º 6
0
int HEAP_EXTRACT_MAX(std::vector A)
{
	if(A.empty())
	{
		return -1;
	}

	int max = A.pop();
	MAX_HEAPIFY(A, 1);

	return max;
}
Exemplo n.º 7
0
void HEAP_SORT(DATA_TYPE *A,int len) {    
    DATA_TYPE temp;    
    int i;    
    BUILD_MAX_HEAP(A,len);    
    for (i=len;i>=2;i--) {        
        temp = A[1];        
        A[1] = A[i];        
        A[i] = temp;        
        HEAP_SIZE --;        
        MAX_HEAPIFY(A,1);    
    }
}
Exemplo n.º 8
0
void HEAP_SORT(int a[],int len)
{
	BUILD_MAX_HEAO(a,len);
	int len2 = len;
	for (int i = len; i > 0;--i)
	{
		int temp = a[0];
		a[0] = a[i];
		a[i] = temp;
		--len2;                                                       //构建最大堆时排除最后一个节点
		MAX_HEAPIFY(a, 0, len2);
	}
}
void HEAPSORT (heap *A)
{
    BUILD_MAX_HEAP(A);
    int i;
    for(i = A->length -1; i>=1; i--)
    {
        int tmp = A->data[0];
        A->data[0] = A->data[i];
        A->data[i] = tmp;
        A->heap_size = A->heap_size -1;
        MAX_HEAPIFY(A, 0);
    }
}
Exemplo n.º 10
0
void HEAPSORT(int* A,int len)
{
	BUILD_MAX_HEAP(A,len);
	int a=0;
	for(int i=0;i<len;i++)
	{
		a=A[0];
		A[0]=A[len-1-i];
		A[len-1-i]=a;
		MAX_HEAPIFY(A,len-1-i,0);
	}

}
Exemplo n.º 11
0
int main(){
	//printf("%d",A[9]);
	//MAX_HEAPIFY(A,5);
	BUILD_MAX_HEAPIFY(A);
	for(i = length;i >=2;i--){
		int tmp = A[i];
		A[i] = A[1];
		A[1] = tmp;
		heap_size = heap_size - 1; //减小堆长度
		MAX_HEAPIFY(A,1);
	 }
	for(i=1;i<11;i++)	
		printf("%d,",A[i]);
	return 0;
}
Exemplo n.º 12
0
void MAX_HEAPIFY(DATA_TYPE *A,int i) {    
    int l,r,largest;    
    DATA_TYPE temp;    
    l = LEFT(i);    
    r = RIGHT(i);    
    if (l<=HEAP_SIZE && A[l]>A[i]) largest = l;    
    else largest = i;    
    if (r<=HEAP_SIZE && A[r]>A[largest]) largest = r;    
    if (largest != i) {        
        temp = A[i];        
        A[i] = A[largest];        
        A[largest] = temp;        
        MAX_HEAPIFY(A,largest);    
    }
}
void MAX_HEAPIFY (heap *A, int i)
{
    int l = LEFT(i);
    int r = RIGHT(i);
    int largest;
    if ((l< A->heap_size) && (A->data[l]>A->data[i]))
        largest = l;
    else
        largest = i;
    if ((r< A->heap_size) && (A->data[r]>A->data[largest]))
        largest = r;
    if (largest != i)
    {
        int tmp = A->data[i];
        A->data[i] = A->data[largest];
        A->data[largest] = tmp;
        MAX_HEAPIFY(A, largest);
    }
}
Exemplo n.º 14
0
void MAX_HEAPIFY(int a[], int i, int len)
{
	int l = 2 * i + 1, r = 2 * i + 2;
	int largest = i;                                             //预设该节点为最大
	if (l <= len &&a[l] > a[i])
	{
		largest = l;
	}
	if (r <= len &&a[r] > a[largest])
	{
		largest = r;
	}
	if (largest != i)
	{
		int temp = a[i];
		a[i] = a[largest];
		a[largest] = temp;
		MAX_HEAPIFY(a, largest, len);
	}
}
Exemplo n.º 15
0
int MAX_HEAPIFY(int A[],int a){
	int l,r; // l <= 左儿子     r <= 右儿子
	l=2*a;
	r=2*a+1;
	//printf("%d \n",A[l]);
	
	if(l <= heap_size && A[l] > A[a])
		largest = l;
	else largest = a;
	if(r <= heap_size && A[r] > A[largest])
		largest = r;
 	if(largest != a){
		tmp = A[a];
		A[a] = A[largest];
		A[largest] = tmp;
		MAX_HEAPIFY(A,largest); //保持堆底下的性质
		
	}
	

}
Exemplo n.º 16
0
int BUILD_MAX_HEAPIFY(int A[]){
	heap_size = length;
	for( i=length/2;i>=1;i--)
		MAX_HEAPIFY(A,i);

}