algorithm--Heap Sorting algorithm (eight)

Source: Internet
Author: User
Tags sort
algorithm--heap Sorting algorithm


Before analyzing heap sequencing, let's review some basic concepts: in computer science, a binary tree is a tree structure with a maximum of two subtrees per node. The subtree is often referred to as the left subtree and the right subtree (subtree). Binary trees are often used to implement a two-fork search tree and a two-fork heap. The complete binary tree: The leaf node can only be present at the bottom and sub-level, and the bottom layer of the node is concentrated in the left-most position of the layer of the two-fork tree (that is, the leaf nodes are arranged from left to right in succession).

A heap is an array that can be seen as an approximate complete binary tree. Each node of the tree corresponds to an element in the array. In addition to the bottom level, the tree is completely full and is padded from left to right. The array A that represents the heap includes two properties: A.length: The number of elements in an array a.heap-size: Indicates how many heap elements are stored in the arrays. In other words, although a[1...a.length] may have data, but only a[1...a.heap-size] is stored in the heap of valid data, here 0=<a.heap-size=< A.length; in the actual algorithm implementation, the Heap-size attribute can be used to remove the maximum value element that has been exchanged.

The heap is also divided into the largest heap and the smallest heap. In the maximum heap, the maximum heap property refers to the node I is satisfied except the root: A[parent (i)]>=a[i]. That is, the value of a node is as large as its parent node. Therefore, the largest element in the heap is stored in the root node and, in any subtree, the value of all nodes contained in the subtree is not greater than the value of the subtree node. The smallest heap is organized in exactly the opposite way: the minimum heap property means that all nodes except the root have a[parent (i)]=<a[i].

Typically, the heap sorting algorithm uses the largest heap, and the minimum heap is used to construct a priority queue.

The heap sorting algorithm implemented in Java is as follows:

	/** * Heap Sort * * @param A */public void Heapsort (int[] a) {int heap_size = a.length-1;
		Buildmaxheap (A, heap_size);
			for (int i = a.length-1; i > 0; i--) {Exchange (A, 0, I);
			heap_size--;
		Maxheapify (A, heap_size, 0);
	 }}/** * * Establish the initial maximum heap * * By the nature of the full binary tree can be launched: If you want to convert the array A[1...N] to the maximum heap, then the sub-array element A[N/2 + 1...N] is the leaf node of the tree, and the leaf node can be seen as a heap of only one element * * @param A * * public void Buildmaxheap (int[] A, int heap_size) {for (int i = A.LENGTH/2-1; I >= 0; i--)
		{maxheapify (A, heap_size, i); }}/** * heap sorting using maximum heap properties * * Maintenance of Maximum heap properties: The largest heap property in this sub-binary tree is maintained with the IMAX node as the root node * * @param A * Sorted array * @param i Dex * Operation Sub-tree root node corresponding subscript value */public void maxheapify (int[] A, int. heap_size, int iMax) {int left = Getlefti

		Ndex (IMAX);//The Left node element of the root node is subscript int right = Getrightindex (IMAX);//The root node is labeled int largest = IMAX;
		if (heap_size >= left && A[left] > A[imax])//determine the underlying element of the root node and the element under the left Dial hand node which is larger; largest stores the subscript of the larger element largest = left; ELSE largest = IMax;  if (heap_size >= right && a[right] > A[largest])//Determine the size of the index of the largest subscript and the element which is the index of the; largest stores the subscript of the larger element largest

		= right; if (IMAX! = largest) {//If largest is different from Imax, the element of the root node is not the largest; an element exchange is made to ensure that the element value of the root node is the largest Exchange (A, IMax, largest) of the root node/left and right child nodes
			; Maxheapify (A, heap_size, largest);//The Maxheapify () function is invoked recursively, so that the subtree with the largest subscript element as the root node also guarantees the heap maximum property}} public void Exchange (int[] A
		, int idex, int idex2) {int temp = A[idex];
		A[idex] = A[idex2];

	A[IDEX2] = temp; /** * * get subscript I corresponding element of the parent element subscript * * @param i * @return * * * public int getparentindex (int imax) {return (IMAX = = 0)? 0: (iMax% 2 = = 0?)
	(IMax-1/2): (IMAX/2)); }/** * * Gets subscript I for the left child element of the parent element of the subscript * * @param i * @return */public int getleftindex (int iMax) {return 2 *
	IMax + 1; }/** * Gets subscript I represents the subscript of the right child element of the parent element * * @param i * @return */public int getrightindex (int iMax) {return 2 * (iMa
	x + 1); }
The complete test code for the heap sorting algorithm is as follows:

public class Sortalgor {public static void main (string[] args) {int[] array = {2,5,10,8,7};
		Sortalgor algorithm = new Sortalgor ();
		Algorithm.heapsort (array);
	Algorithm.arrayprint (array);
		}/** * Heap sort * * @param A */public void Heapsort (int[] a) {int heap_size = a.length-1;
		Buildmaxheap (A, heap_size);
			for (int i = a.length-1; i > 0; i--) {Exchange (A, 0, I);
			heap_size--;
		Maxheapify (A, heap_size, 0);
	 }}/** * * Establish the initial maximum heap * * By the nature of the full binary tree can be launched: If you want to convert the array A[1...N] to the maximum heap, then the sub-array element A[N/2 + 1...N] is the leaf node of the tree, and the leaf node can be seen as a heap of only one element * * @param A * * public void Buildmaxheap (int[] A, int heap_size) {for (int i = A.LENGTH/2-1; I >= 0; i--)
		{maxheapify (A, heap_size, i); }}/** * heap sorting using maximum heap properties * * Maintenance of Maximum heap properties: The largest heap property in this sub-binary tree is maintained with the IMAX node as the root node * * @param A * Sorted array * @param i Dex * Operation Sub-tree root node corresponding subscript value */public void maxheapify (int[] A, int. heap_size, int iMax) {int left = Getlefti Ndex (IMax);//The left node of the root nodeThe element subscript int right = Getrightindex (IMAX);//The node of the root node is labeled int largest = IMAX;
		if (heap_size >= left && A[left] > A[imax])//determine the underlying element of the root node and the element under the left Dial hand node which is larger; largest stores the subscript of the larger element largest = left;

		else largest = IMax;  if (heap_size >= right && a[right] > A[largest])//Determine the size of the index of the largest subscript and the element which is the index of the; largest stores the subscript of the larger element largest

		= right; if (IMAX! = largest) {//If largest is different from Imax, the element of the root node is not the largest; an element exchange is made to ensure that the element value of the root node is the largest Exchange (A, IMax, largest) of the root node/left and right child nodes
			; Maxheapify (A, heap_size, largest);//The Maxheapify () function is invoked recursively, so that the subtree with the largest subscript element as the root node also guarantees the heap maximum property}} public void Exchange (int[] A
		, int idex, int idex2) {int temp = A[idex];
		A[idex] = A[idex2];

	A[IDEX2] = temp;
	/** * * get subscript I corresponding element of the parent element subscript * * @param i * @return * * * public int getparentindex (int imax) {return IMAX;  }/** * * Gets subscript I for the left child element of the parent element of the subscript * * @param i * @return */public int getleftindex (int iMax) {return 2 *
	IMax + 1; /** * gets subscript I for the right child of the parent element of the subscript * * @param i * @return */public int getrightindex (int imax) {return 2 * (IMax + 1);
		} public void Arrayprint (int[] array) {System.out.print ("[");
		for (int i = 0; i < Array.Length; i++) {System.out.print (Array[i] + ",");
	} System.out.println ("]");
 }
}
It would have been a bit of an illustration to show the operation of the heap sorting algorithm, but the website seems to have a problem, the picture inserted cannot be displayed. Fill it up in the back ~ ~





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.