Classical algorithm (7) heap and heap sorting

Source: Internet
Author: User

Heap sort and quick sort, merging sort are all the common sorts of time complexity O (N*LOGN). Before learning heap sorting, first explain what is the data structure of the two-fork heap.

The definition of a binary heap

The two-fork heap is a complete binary tree or a nearly complete binary tree.

The two-fork heap satisfies two characteristics:

1. The key value of a parent node is always greater than or equal to (less than or equal to) the key value of any child node.

2. The Saozi right subtree of each node is a binary heap (both the maximum heap and the smallest heap).

The maximum heap when the key value of a parent node is always greater than or equal to the key value of any one child node. The minimum heap when the key value of a parent node is always less than or equal to the key value of any one child node. The following illustration shows a minimum heap:

As a result of several other heaps (two-item heap, Fibonacci Ponachi, etc.) used less, generally two-fork heap is referred to as the heap.

Storage of the heap

Generally, arrays are used to represent the heap, and the parent node of the I node is labeled (i–1)/2. Its left and right sub nodes are labeled 2 * i + 1 and 2 * i + 2 respectively. such as the No. 0 node left and right sub-node subscript 1 and 2 respectively.

Heap operation--Insert Delete

The following first given the "data structure C + + language description" of the smallest heap of building insert Delete diagram, and then give me the implementation code, it is best to see the map before you look at the code.

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.