Internal sort: Heap Sort

Source: Internet
Author: User
Tags sort

Objective

The average time complexity for heap sort, quick sort, and merge sort (which will write the two sorting algorithms in the next article) is O (N*logn). to figure out how to sort the heap, you need to understand the two-fork heap of this data structure first. Instead of fully describing all the operations of the binary heap, this article focuses on the operations to be used in the heap sort. For example, when we build a heap, we can use a heap insert (insert the element into the appropriate position, so that the new sequence still conforms to the definition of the heap, insert the elements one by one into the heap, but we do not have to do this at all, we have fewer ways to do it, and later you will see that we are basically using only the heap deletion operations, More specifically, you should delete the root node of the heap, and then continue to adjust the remaining elements to the heap's operations. Let's look at the definition of the binomial heap.

Two-fork Heap

The binary heap is actually a completely binary tree with a special nature, the special property here means:

1, the binary heap of the parent node is always greater than or equal to (or less than equal to) the value of their children;

2, each node of the left and right subtree is one such two-fork heap.

If the value of a parent node of a binary heap is always greater than the value of its left and right child, then the two-fork heap is the largest heap, or the smallest heap. When we sort, if we want to sort the order from small to large, you need to select the largest heap, on the contrary, select the smallest heap, this through the face of the heap sorting analysis, you will experience.

Heap Sort

The definition of the two-fork heap indicates that the top element of the heap (that is, the root node of the binary heap) must be the maximum or minimum value in the heap. So if we output the top element of the heap, we will adjust the remaining elements to a two-fork heap, then output the heap top element again, then adjust the remaining elements to a two-fork heap, and execute the process repeatedly. This allows the output of an ordered sequence, which we call a heap sort.

Since our input is a unordered sequence, we have to solve the following two problems in order to implement heap sequencing:

1. How to construct a two-fork heap with a disordered sequence;

2. After removing the top element of the heap, how to adjust the remaining elements to a two-fork heap.

For the first question, it may be obvious to think of a heap insert, one by one inserting the element, adjusting the position of the element after each insertion, so that the new sequence remains a two-fork heap. This is generally a bottom-up adjustment operation, that is, the element to be inserted behind the binary heap, and then gradually up to compare it with the parent node, and then adjust the position. But as the preface says, we don't have to insert one node at a point at all, so what do we do? We need to solve the second problem first, solve the second problem, the first question will be solved.

Adjust the two-fork heap

To analyze the second question, we first give the following prerequisites:

1, we sort the goal is from small to large, so we use the largest heap;

2. We keep the elements in the two-fork heap in the order of sequence traversal in a one-dimensional array, and the root node in the array has a position ordinal of 0.

Thus, if a node is in an array in the position ordinal I, then its left and right child's position ordinal number is 2i+1 and 2i+2 respectively.

To make the adjustment process easier to understand, we use the following two-fork heap to analyze (note that the following analysis, we do not use an additional array to store each of the heap top data removed):

Here, the number of elements in array A is 8, and it is obvious that the maximum value is A0, in order to achieve the sorted elements in order from small to large, we can swap the last element of the two-fork heap A7 with the A0, so that A7 save the maximum value in the array, and at this point the two-fork tree becomes the following:

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.