Fortune left France 3 basis class2-- topic heap heapInsert, heapify, heapsort C ++ implementation

1. Basics

1. There are two complete binary tree:
5
/ \
12
/ \ /
3426
full binary
5
/ \
1 2
/
1
from left to right ordered binary
2. The binary tree may be completely equivalent to heap that arrays can be used to achieve
wherein the i-th node left child: 2 i +. 1, the i-th node of the right child: 2 i + 2, the parent node of the i-th node: (i-. 1) / 2
3. large root stack: refers to any sub- the maximum value of the tree node is the head of
small heap root: Min refers to any sub-tree of the node is the head of the

2.heapInsert: new node to join in the process of adjustment is large root heap

The array into a large root heap, stack structure established

analysis

If the current element is greater than the insertion element of its parent node, then the switching position of the parent node of the current node. Then the size of the parent node after the inspection element inserted node with the new replacement location, even if greater than or continue to exchange.
eg. Example [2,1,3,6] is converted into a large heap root
positions 0,1,2,3
(1) 2 is inserted, the parent node (0-1) / 2 = 0 position, and they do not move themselves;
(2) 1 is inserted, the parent node (1-1) / 2 = 0 position, 1 <2, does not move;
(3) 3 is inserted, the parent node (2-1) / 2 = 0 position, 3> 2, change position; this 3 position becomes 0, the parent node (0-1) / 2 = 0 position, and they do not move themselves;
this case:
3
/
12
(4) 6 is inserted, the parent node (3-1) / 2 = position 1, 6> 1, the exchange position; a position 6 at this time becomes a parent node (1-1) / 2 = 0 position; 6> 3, continue to exchange;
6
/
32
/
1

Core code

(Index-1) / 2 is the location of the parent node of the index

void heapinsert(int arr[],int index)
{
	while(arr[index] > arr[(index-1)/2])
	{
		swap(arr[index],arr[(index-1)/2]);
		index = (index-1)/2;
	}
}

Use a for loop through the array, call heapinsert

for(int i = 0;i < length;i++ )
	{
		heapinsert(arr,i);//用for循环传入要处理的index
	}

The complete code

#include<iostream>

#define length 5
using namespace std;

void swap(int &a,int &b)
{
	int temp = a;
	a = b;
	b = temp;

}
void heapinsert(int arr[],int index)
{
	while(arr[index] > arr[(index-1)/2])
	{
		swap(arr[index],arr[(index-1)/2]);
		index = (index-1)/2;
	}
}

int main()
{
	//int arr[length] = {2,1,3,6,0,4};
	int arr[length] = {3,4,5,1,2};
	int heapsize = length;
	for(int i = 0;i < length;i++ )
	{
		heapinsert(arr,i);//用for循环传入要处理的index
	}


	system("pause");
	return 0;
}

time complexity

Plus a number required comparative height of the tree views, O (logN), when 0 to i-1 was added a few need to compare log (I-1) times; the number N need log1 + log2 + ... + log (N-1) = log (N)

3.heapify: Suppose the array during a smaller value, readjusted to stack large root

analysis

1. Find the maximum value of the number of children left and right, the maximum value is compared with the change, if the change is greater than the maximum value of the left and right children, the switching of two numbers, and then to find the left and right children exchange, comparison continues.
2. To continue the comparison condition: children are not left out of bounds.

Core code

And comparing the current number of children around, find the maximum value assigned to the largest; if the current number is the largest jump, if not, the two exchanged numbers, update index, and children left position, continue to compare.

void heapify(int arr[],int index,int heapsize)
{
	int left = index * 2 + 1; 
	while(left < heapsize)
	{
		int largest = left + 1 < heapsize && arr[left]<arr[left + 1] ? 
			left + 1:left;
		largest = arr[largest] > arr[index] ? largest : index;
		if(largest == index)
			break;
		swap(arr[largest],arr[index]);
		index = largest;
		left =  index * 2 + 1;
	}
}

The complete code

#include<iostream>

#define length 6
using namespace std;

void swap(int &a,int &b)
{
	int temp = a;
	a = b;
	b = temp;

}
void heapify(int arr[],int index,int heapsize)
{
	int left = index * 2 + 1; 
	while(left < heapsize)
	{
		int largest = left + 1 < heapsize && arr[left]<arr[left + 1] ? 
			left + 1:left;
		largest = arr[largest] > arr[index] ? largest : index;
		if(largest == index)
			break;
		swap(arr[largest],arr[index]);
		index = largest;
		left =  index * 2 + 1;
	}
}

int main()
{
	//int arr[length] = {2,1,3,6,0,4};
	int arr[length] = {6,5,4,3,5,2};
	int heapsize = length;
	arr[0] = 1;
	heapify(arr,0,heapsize);


	system("pause");
	return 0;
}

4. heapsort

analysis

heapinsert combined heapify.
(1) becomes so large root stack array (heapinsert);
(2) keeping the last top of the stack and the exchange heap size minus one (save maximum)
(3) re-stack becomes large root, elements corresponding to the top of the stack then small rearrangements (heapify)
(. 4) is stopped until a heap size.

Core code

1.heapinsert

void heapinsert(int arr[],int index)
{
	while(arr[index] > arr[(index-1)/2])
	{
		swap(arr[index],arr[(index-1)/2]);
		index = (index-1)/2;
	}
}

2.heapify

void heapify(int arr[],int index,int heapsize)
{
	int left = 2*index+1;
	while (left < heapsize)
	{
		int largest = left+1<heapsize && arr[left+1]>arr[left]?
			left+1:left;
		largest = arr[index] < arr[largest]?largest:index;
		if(index == largest)
			break;
		swap(arr[index],arr[largest]);
		index = largest;
		left = 2*index+1;
	}
}

3.heapsort

void heapsort(int arr[],int heapsize)
{
	while(heapsize > 1)
	{
		swap(arr[0],arr[heapsize-1]);
		heapsize--;
		heapify(arr,0,heapsize);
	}
}

The complete code

#include<iostream>
#include<time.h>
#define length 10
using namespace std;

void swap(int &a,int &b)
{
	int temp = a;
	a = b;
	b = temp;
}

void heapify(int arr[],int index,int heapsize)
{
	int left = 2*index+1;
	while (left < heapsize)
	{
		int largest = left+1<heapsize && arr[left+1]>arr[left]?
			left+1:left;
		largest = arr[index] < arr[largest]?largest:index;
		if(index == largest)
			break;
		swap(arr[index],arr[largest]);
		index = largest;
		left = 2*index+1;
	}

}


void heapsort(int arr[],int heapsize)
{
	while(heapsize > 1)
	{
		swap(arr[0],arr[heapsize-1]);
		heapsize--;
		heapify(arr,0,heapsize);
	}
}


void heapinsert(int arr[],int index)
{
	while(arr[index] > arr[(index-1)/2])
	{
		swap(arr[index],arr[(index-1)/2]);
		index = (index-1)/2;
	}
}


int main()
{
	srand((unsigned)time(NULL));
	
	int arr[length]  ;
	int heapsize = length ;
	cout<<"arr = ";
	for(int i = 0;i<heapsize;i++)
	{
		arr[i] = rand()%10;
		cout<<arr[i]<<" ";
		heapinsert(arr,i);
	}
	cout<<endl<<"arr1 = ";

	heapsort(arr,heapsize);

	for(int i = 0;i<length;i++)
	{
		cout<<arr[i]<<" ";
	}
	cout<<endl;

	//system("pause");
	return 0;
}

time complexity

O (N * logN), does not consider the establishment of the process stack, each sorting logN, the N row.

Published 51 original articles · won praise 1 · views 1394

Guess you like

Origin blog.csdn.net/shi_xiao_xuan/article/details/103508459