The algorithm idea of deep analytic heap sorting and the realization of Java code demo _java

Source: Internet
Author: User

First, basic knowledge
What we usually call a heap is a two-fork heap, a binary heap called a complete binary tree or an approximate complete binary tree. The two-fork heap is divided into the maximum heap and the smallest heap.
Heap Ordering (heapsort) is a sort algorithm designed by using the data structure of a heap, which is a sort of selection. The elements of the specified index can be quickly positioned using the characteristics of the array. The array can get the element directly from the index, the time complexity is O (1), which is constant, so it is highly efficient for the value.
The characteristics of the maximum heap are as follows:

    • The key value of the parent node is always greater than or equal to the key value of any one child node
    • The Saozi right subtree of each node is a maximum heap.

The characteristics of the minimum heap are as follows:

    • The key value of the parent node is always less than or equal to the key value of any one child node
    • The Saozi right subtree of each node is a minimum heap.

Second, the algorithm thought
1. The algorithm idea of the Max Heap is:
first set the initial r[0...n-1] into the largest heap, at this time the unordered heap, and the top of the heap is the largest element
Then the last record of the heap top r[0] and the disordered region are exchanged, thus the new disordered region R[0...n-2] and the ordered region r[n-1 are obtained, and the R[0...n-2].keys≤r[n-1].key
Because the former r[0...n-2] may not satisfy the maximum heap's nature after swapping, the previous r[0...n-2] is the maximum heap until the last element of R[0 is adjusted to complete.
After the maximum heap sort is completed, it is actually an ascending sequence, each time the heap is to get the largest element and then swap with the last element of the current heap, so the resulting sequence is the ascending sequence.
2. The idea of the minimum heap algorithm is:
first the initial r[0...n-1] is built into the smallest heap, at this time the unordered heap, and the top element of the heap is the smallest element
Then the heap top r[0] is exchanged with the last r[n-1 of the unordered region, thereby obtaining a new unordered heap r[0...n-2] and an ordered heap r[n-1, and satisfying R[0...n-2].keys >= R[n-1].key
Because the former r[0...n-2] may not meet the nature of the minimum heap after the swap, the previous r[0...n-2] is the minimum heap until only the last element of R[0 is adjusted to complete
When the minimum heap sort is finished, it is actually a descending sequence, each time the heap is to be given the smallest element and then exchanged with the last element of the current unordered heap, so the resulting sequence is descending.
Tip: The process of heap sequencing, in fact, is to continuously expand the ordered area, and then continuously reduce the disorder area, until only ordered area of the process.

Third, the sequencing process analysis
because the algorithm is more abstract, here directly by a small example to illustrate the process of heap ordering. Here we use this unordered sequence to sort the heap by the maximum heap, and the resulting sequence is the ascending sequence (ASC).
Unordered sequence: 89,-7,999,-89,7,0,-888,7,-7
The first step: Initialize the largest heap:

The second step: the maximum element 999 of the top of the heap is exchanged with the last element of the disordered region, so that 999 is ordered. After swapping, 70% is the top of the heap, because-7 is not the largest element in the unordered area, so it is necessary to adjust the unordered area so that the maximum value 89 in the unordered area becomes the top of the heap, so-7 is exchanged with 89. After swapping, the right subtree of 89 does not satisfy the maximum heap, so the right subtree is adjusted to the maximum heap, so-7 is exchanged with 0, as in the following figure:

As you can see from the diagram, when-70% 89 is switched, the top is the largest element, but-7 of the left child is 0, and the right child is-888, due to -7<0, causes-7 This node does not satisfy the nature of the heap, so need to adjust it. So, 0 is exchanged with-7.
It then repeats the second step, until it becomes an orderly area.
Finally: The ascending sequence is obtained

Four, time complexity
the time of the heap sort is mainly composed of the time cost of establishing the initial heap and the repeated adjustment of the heap. Because the heap order is unstable, the complexity of the time it has to twist will be larger according to the actual situation, so it can only take the average time complexity.
Average time complexity is: O (n * log2 (n))
Heap ordering time-consuming operations are: initial heap + repeatedly adjust the heap, the time complexity of the following:
1. Initial heap: Each parent node will be compared with the left and right child nodes for up to 2 times and 1 times, so the complexity is related to the number of parent nodes. According to the 2x <= N (x is the number of n elements that can be binary, that is, the number of parent nodes), we get x = log2n. That is, O (log2n)
2. Repeatedly adjust the heap: As the initialization heap process, the array comparison results are recorded, so heap ordering is not sensitive to the order of the original sequence array, the best case and worst-case scenario. The n-1 heap top element needs to be extracted, and the Rebuild Heap (O (Rebuild heap) < O (initial heap) is required each time a heap top element is fetched. So less than O (n-1) * O (log2n)
Use recommendations:
Because of the number of times the initialization heap needs to be compared, heap sorting is more appropriate for situations where the data volume is very large (millions of data or more). Because efficient fast sorting is based on recursion, a stack overflow error occurs when the volume of data is very large.

V. Java SAMPLE Code

public class heapsort{private static int[] sort=new int[]{1,0,10,20,3,5,6,4,9,8,12, 17,34,11};
  public static void Main (string[] args) {buildmaxheapify (sort);
  Heapsort (sort);
 Print (sort); private static void Buildmaxheapify (int[] data) {///No child nodes are required to create the maximum heap, starting with the last parent node int Startindex=getparentindex (Data.len
GTH-1);
  Create the largest heap from the end, each time the correct heap for (int i=startindex;i>=0;i--) {maxheapify (data,data.length,i); /** * Create Maximum heap * * @paramdata * @paramheapSize need to create the maximum heap size, typically used in sort, because the maximum value is at the end and the end is no longer in the maximum heap * @paramindex currently needs to create the most The location of the large heap/private static void Maxheapify (int[] data,int Heapsize,int index) {//The current point is compared to the left and right child nodes int Left=getchildleftindex (
  index);

  int Right=getchildrightindex (index);
  int largest=index;
  if (Left 

Related Article

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.