Introduction to algorithms-Chapter 2-heap sorting

Source: Internet
Author: User

1.In-situ sortingIt refers to the sorting by comparison and exchange in the original sorting data without applying for additional space. For example, fast sorting and heap sorting are all in-situ sorting, Merge Sorting, and count sorting.

Stable sortingThat is, if the two numbers are the same, the sorting results for them will not change their relative order. 2 Stack definition:
 

 N key word sequences Kl, K2 ,..., Kn is called a heap, and only when the sequence meets the following properties (referred to as heap properties ):

 (1) ki <= k (2i + 1) and ki <= k (2i + 2) (1 ≤ I ≤
N), of course, this is a small root heap, and the big root heap is replaced with the "> =" sign. // Ki is equivalent to a non-leaf node of a binary tree. K2i is the left child and k2i + 1 is the right child.

 If we regard the vector R [1. n] stored in this sequence as a storage structure of a Complete Binary Tree, the heap is essentially a Complete Binary Tree that meets the following requirements:

 The keywords of any non-leaf node in the tree are not greater than (or not less than) the keywords of its left and right children (if any) nodes.

Heap sorting:

Heap sorting is a kind of sorting. Is an unstable sorting method. The time complexity is O (nlogn ).

Heap sorting is characterized by the sequential storage structure of a Complete Binary Tree in the sorting process. The inherent relationship between parent and child nodes in the Complete Binary Tree is used, select the record with the maximum or minimum keyword in the unordered area.

Basic Idea of heap sorting:

1. Create an array to be sorted as a large root heap. The top element of the heap is the largest element in the heap.

2. Swap the top elements of a large root heap with the last element in the unordered zone, enter the last position of the unordered zone as an example, and then change the new unordered zone to a large root heap. Repeated operations: unordered areas are decreasing, and ordered areas are increasing.

Basic nature of a Complete Binary Tree:

The array contains n elements, I is a node, and 1 <= I <= n/2 means that the last half of the array is a leaf node.

 I parent node location: I/2

 I left subnode position: I * 2

 I right sub-node position: I * 2 + 1

Length [A] is the number of elements in the array, and heap-size [A] is the number of elements stored in heap.

Below I follow the algorithms provided in the introduction to algorithms, and use c ++ to implement heap sorting and debugging.

# Include <iostream> using namespace std; int data [10] = {151,138,160, 63,174,169, 79, 78}; void max_heapify (int data [], int I, int heapsize) // adjust the subtree with a node as the root node to a large top heap {int l = 2 * I + 1; int r = 2 * I + 2; int largest = I; if (l 


 

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.