Java code as an example to explain the nature and basic operation of the heap and sorting method _java

Source: Internet
Author: User

The nature of the heap
The heap is a complete binary tree, in fact, can be implemented through an array, its most important property is that any node is less than (greater than) equal to its child nodes. The smallest heap of the root node is called the minimum heap, and the largest heap of the root node is called the maximum heap. The following illustration shows an example of a maximum heap and its array representation, which visually shows that each node is larger than its children.

As you can see in the previous illustration, a fully binary tree node can be sorted sequentially from the root node number 1, corresponding to the index in array a (note that the subscript is starting at 1). Given a node I, we can easily get it's left child is 2i, right child is 2i+1, parent node is I/2

Basic operation of the heap
The heap has two basic operations (for example, the smallest heap below):
Insert Element K: Add K directly to the array at the end, and then bubble up (bubble-up) to adjust the heap. Bubbling up: The element to be adjusted is compared to its parent node, exchanged if it is greater than its parent, until the nature of the heap is restored.
Extract Maximum value: The maximum is the root element. It is then deleted, so that the root element = the last leaf node element, and then bubbles downward from the root element (Bubble-down) to adjust the heap. Bubbling down operation: each should be adjusted from the node, its left and right children in a total of three nodes select the smallest child nodes to exchange (if the smallest is itself without swapping), until the nature of the recovery heap.
In practice, it is often necessary to build an unordered array of n elements, and the construction method in the following heap class will show how to build the heap with _bubbledown downward bubbling adjustment. The heap is essentially a completely binary tree with a tree height of lognlog n, and the time-consuming operations of each basic operation are bubbling to meet the nature of the heap, so their time complexity is O (NLOGN) O (nlog N).
Java Example:

Floating public
void swim (int k) {while
  (k/2>=1 && Less (pq[k/2],pq[k))) {
    exch (pq,k/2,k);
    K=K/2
  }
}
Sink
private void sink () {
  int k=1;
  while (2*k<n) {
    int j=2*k;
    if (less (pq[j],pq[j+1])) j + +;
    if (less (pq[k],pq[j])) Exch (pq,k,j);
    else break;
    K = j;
  }
}

The principle of heap sort implementation
is divided into two steps:
1. Order the array into a binary heap
2. Swap the location of the root node and the last node, and then sink the root node.

Realize:
Maybe my code is slightly out of line with the animation above, but the basics are pretty much the same.

public class Heapsort extends Basesort {

  private int N;
  @Override public
  void sort (comparable[] a) {
    N =a.length-1;
    int k = N/2;
    while (k>=1) {
      sink (a,k);
      k--;
    }
    k = 1;
    while (k<=n) {
      exch (a,k,n--);
      Sink (a,k);}}


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.