Implementation of heap sequencing-C language

Source: Internet
Author: User

Heap

The heap is an array object of a completely binary tree. Each layer of the tree is full, except that the last layer may be Zuozi (from the beginning of a node).

Given the node I, it is easy to compute the location of the parent and child nodes.

Parent (i) =floor (I/2): I/2 down again

Leftchild (i) =2* (i+1)-1: Since I started with 0, this and the C language array begin with 0. You can use the left shift operation instead of *, i.e. leftchild (i) = (i+1) <<1-1;

Rightchild (i) = 2* (i+1): Rightchild (i) = (i+1) <<1;

Heap Sort

The time complexity of heap sequencing is O (NLGN), which is a more efficient one. It uses the largest heap. The maximum heap means that the value of the parent node >= the child's value. Then the NO. 0 node must be the maximum value for the heap. So the idea of heap sequencing is to move the maximum value out of each loop and then re-establish the maximum heap from the remaining nodes.

The first step: build the structure of the heap.

typedef struct heap_t{
	int *arr;	       Point for a array to store heap value.
	int heapmaxindex;	Heap Element Max index number
	int arrlength;	Array length of arr
	
}heap;

Where the arr pointer points to an array of stored heap data.

Heapmaxindex is the largest ordinal number of an array. If the array is defined as a[10], then the Heapmaxindex value should be 9.

Step Two: Keep the nature of the heap.

This step is the basis for heap sorting. Here, the function is written as a function named void Maxheapify (Heap *hp, unsigned int nodei), which is used to make an array of arrays that conform to the nature of the heap. The time complexity is O (h) and H is the height =LGN (n is the number of nodes) of the two-tree tree to which the heap belongs.

Thought is: From a node I, and his child leftchild (i), rightchild (i) found the largest, and then its index stored in largest. If I is the largest. Then I as the root of the subtree is already the largest heap, the program is over.

Otherwise, one of the child nodes of I has the maximum element, then the value of I is exchanged with the value of the largest. A node labeled largest is a parent node after swapping, so it is possible to violate the heap nature and therefore recursively call the function.

void Maxheapify (Heap *hp, unsigned int nodei)
{
	unsigned int l = (nodei+1) << 1-1;	Left child = 2i-1,-1?: arr[0..n-1]
	unsigned int r = (nodei+1) << 1;	Right child = 2i
	unsigned int largest = 0;
	int Heapmaxi = hp->heapmaxindex;

	if (l <= heapmaxi && hp->arr[l] > Hp->arr[nodei])
		largest = l;
	else
		largest = Nodei;
	
	if (R <= heapmaxi && Hp->arr[r] > hp->arr[largest])
		largest = R;

	if (Largest!=nodei)
	{	
		//exchange 
		int tmp;
		TMP = hp->arr[largest];
		Hp->arr[largest] = hp->arr[nodei];
		Hp->arr[nodei] = tmp;
		
		Maxheapify (hp,largest);
	} else{return
	}
	
}



The third step is to create a heap using the Maxheapify function

For a heap of 1 numbers n, it can be analyzed from the graph above, which is the parent node before n/2-1. After all is the leaf node, we only need to maxheapify on the parent node can be.

N/2 can be n>>1 using the right shift operation.

Heap *createheap (int *arrp, int arrlength,heap *heap)
{
 int i;
 Heap->arr = ARRP;
 Heap->heapmaxindex = arrLength-1;
 Heap->arrlength = arrlength;

 For a heap a[0..n-1]; a[(N/2). N-1] All are leaves for
 (i = arrlength>>1-1; i >=0; i--) 
  maxheapify (heap,i);
 return heap;
}


Fourth step: Heap Sort

Set the heap of the array as a[0..n-1], call the maxheapify function can get the maximum value, and then the maximum and n-1 interchange, the heap size Heapmaxindex minus 1, call Maxheapify again, and get the maximum value, stored in the a[ 0], again and a[n-2] swap, the size of the heap again minus one, so that the cycle down, know the size of the heap is 0. Then we get an array of sorted order from small to large.



void Heapsort (Heap *hp)
{
	int tmp;
	int last;
	while (hp->heapmaxindex>0)
	{last
		= Hp->heapmaxindex;
		Exchange
		tmp = hp->arr[last];
		Hp->arr[last] = hp->arr[0];
		HP->ARR[0] = tmp;
		Hp->heapmaxindex-= 1;
		Maxheapify (hp,0);	Make heap from root node 0 to Heapmaxindex 
	}
	
}


Test heap Sorting

void Printarr (int *p, int size)
{
	int i;
	for (i=0 i<size;i++)
	{
		printf ("%d", P[i]);
	}

int main ()
{
	int a[]={15,25,32,23,1,-4,35,2,-85,42,0,12,26,56,45,12,145,17,25,21};
	Printarr (a,20);
	printf ("\ n");

	Heap HPA,*PHPA;
	PHPA =  createheap (A,20,&HPA);
	Heapsort (PHPA);

	Printarr (a,20);
	Putchar (' \ n ');
	return 0;	
}


The results are:-85-4 0 1 2 12 12 15 17 21 23 25 25 26 32 35 42 45 56 145

Heap Sort Application:

Priority queues, event-driven mode response queues, and so on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.