Example #1
0
File: code.c Project: WingT/CLRS
int buildminheap(struct heap*q,int heapsize)
{
	int i;
	for (i=heapsize/2;i>=1;i--)
		minheapify(q,i,heapsize);
	return 0;
}
Example #2
0
File: code.c Project: WingT/CLRS
struct node * extractmin(struct heap *q,int heapsize)
{
	struct heap tmp;
	tmp=q[1];
	q[1]=q[heapsize];
	minheapify(q,1,heapsize-1);
	return tmp.hnode;
}
Example #3
0
void buildminheap(Heap heap)
{
    int i;
    for(i = heap.length - 1; i >= 0; i--) 
    {
        minheapify(heap, i);
    }
}
int minvalue(node arr[],int* size)
{
    int min = arr[1].value;
    swap(arr,1,*size);
    *size = *size - 1;
    minheapify(arr,1,*size);
    return min;
}
Example #5
0
HeapNode *delminimal(Heap *heap)
{
    HeapNode *minimal = heap->a[0];
    //sawp the last element with the minimal element
    //then dec the heap size
    heap->a[0] = heap->a[--(heap->length)];
    minheapify(*heap, 0);
    return minimal;
}
Example #6
0
int extract_min(int n,int a[n]){
	if(n<1){
		printf("underflow");
		return -1;
	}
	int min;
	min=a[1];
	a[1]=a[n];
	n=n-1;
	minheapify(n,a,1);
	return min;
}
Example #7
0
void minheapify(int n,int a[n],int i){
	int l,r,min=i;
	l=2*i;
	r=l+1;
	if(l<=n && a[l] < a[i])
		min=l;
	if(r<=n && a[r] < a[l])
		min=r;
	if(min!=i) {
		a[min] = a[i] + a[min] - (a[i] = a[min]);
		minheapify(n,a,min);
	}
}
void minheapify(node arr[],int i,int size)
{
    int left = 2*i;
    int right = 2*i+1;
    int minimum = i;
    if(left<=size && arr[left].prior<arr[minimum].prior){
        minimum = left;
    }
    if(right<=size && arr[right].prior<arr[minimum].prior){
        minimum = right;
    }
    if(i!=minimum){
        swap(arr,i,minimum);
        minheapify(arr,minimum,size);
    }
}
Example #9
0
void minheapify(Heap heap, int i)
{
    int l = LEFT(i);
    int r = RIGHT(i);
    int minimal = i;
    if(l < heap.length && heap.a[l]->key < heap.a[i]->key) 
        minimal = l;
    if(r < heap.length && heap.a[r]->key < heap.a[minimal]->key)
        minimal = r;
    if(minimal != i)
    {
        HeapNode *temp = heap.a[i];
        heap.a[i] = heap.a[minimal];
        heap.a[minimal] = temp;
        minheapify(heap, minimal);
    }
}
Example #10
0
File: heapsort.c Project: Sykel/C--
int main()
{
  int arr[MAX_SIZE], size, i;

  printf("Enter size of the array...\n");
  scanf("%d", &size);

  printf("Enter the array of integers...\n");

  for(i=0;i<size;i++)
	scanf("%d", &arr[i]);
  
  printf("Input array is:\n");
  display(arr, size);


 //ascending order
 for (i = 0; i < size; i++)
 {
	heapify(arr, 0, size-i);
	swap(arr+0, arr+(size-i-1));
 }
 printf("\nAscending order:\n");
 display(arr, size);

 //descending order
 for (i = 0; i < size; i++)
 {
	minheapify(arr, 0, size-i);
	swap(arr+0, arr+(size-i-1));
 }
 printf("\nDescending order:\n");
 display(arr, size);

 return 0;
}
Example #11
0
void buildheap_min(int n,int a[n]){
	int j;
	for(j=n/2;j>=1;j--){
		minheapify(n,a,j);
	}
}