C # heap sorting of sorting algorithms

Source: Internet
Author: User

I. Basic Concepts

Heap: this refers to a data structure, not the place where we mentioned in C # used to store referenced objects. It can be regarded as a Complete Binary Tree.

In order to store the heap with arrays, each node is marked in order. In fact, we can use a simple formula to obtain the index of the parent node, left child, and right child:

Parent (I) =

Left (I) = 2i

Right (I) = 2i + 1

Max heap and Min heap:The maximum heap refers to the heap where the value of all parent nodes is greater than that of the child node, which meets the following formula:

A [parent [I] A [I] (A refers to the array storing the heap)

The smallest heap is opposite.

The maximum heap and the minimum heap are the key to heap sorting. We can see that the root node of the maximum heap is the largest node in the heap. Therefore, as long as we construct the largest (small) heap, the largest (small) element will be obtained, and then the largest (small) heap will be constructed for the remaining elements, the second (small) element can be extracted, and so on until the sorting is complete.

2. Construct the largest (small) Heap
We have learned that constructing the largest (small) heap is the key to heap sorting. Let's take a look at how to construct the largest heap.
Everything starts hard. First, let's look at a special situation: the left and right subtree of the heap root node are already the largest, but the root node is smaller than the child node, of course, this heap does not meet the definition of the maximum heap. To make the heap the largest heap, follow these steps:
(1) swap the root node with the largest of the left and right children
(2) After the switch, the left or right subtree may not be the largest heap, but since the entire left (right) subtree is the largest heap at the beginning, the problem returns to the initial state, so you only need to repeat it to get the maximum heap.
A solution has been found for the special heap above, but what about the heap in the general sense?
We can choose from the bottom up structure: leaf nodes are the special largest heap. For example, leaf nodes a and B have their parent nodes p;, B must have been the largest heap. This is to ensure that the subtree composed of a, B, and p is the largest heap. This heap is very familiar, isn't it? Yes, it is the special heap mentioned above. After the subtree composed of a, B, and p becomes the largest heap, we can make the subtree, the parent node of the subtree, And the sibling subtree (or node) similarly) the new subtree is the largest heap, and so on, eventually the heap becomes the largest heap.

The minimum heap for solving is similar.
Iii. Implementation
Complete code:Copy codeThe Code is as follows: namespace HeapSort
{
Using System;
Class Program
{
Static int heapSize = 0;
Static void Main (string [] args)
{
Var heap = new [] {-1, 10, 5, 12, 77, 54, 7, 34, 23, 11}; // For convenience, no elements are stored at index 0 (or useless elements are stored)
HeapSize = heap. Length-1;
BuildMaxHeap (heap );
For (var I = heap. Length-1; I> = 2; I --)
{
// 1. After the maximum heap is built, the first element and the last element are exchanged;
// 2. For the first time, a new heap is formed by the elements from Index 1 to length-1. For the second time, 1 to length-2 until the last two elements form the heap.
// 3. Each new heap can maintain the maximum heap feature except the root node. Therefore, you only need DoBuildMaxHeap (heap, 1) to obtain the new maximum heap.
Swap (heap, 1, I );
HeapSize --;
MaxHeapfy (heap, 1 );
}
Foreach (var I in heap)
Console. Write (I + "");
}
Static void BuildMaxHeap (int [] heap)
{
For (var I = (heap. Length-1)/2; I> = 1; I --)
{
MaxHeapfy (heap, I );
}
}
Static void MaxHeapfy (int [] heap, int index)
{
Var largerItemIndex = index;
Var leftChildIndex = index <1;
Var rightChildIndex = (index <1) + 1;
If (leftChildIndex <= heapSize & heap [leftChildIndex]> heap [index])
{
LargerItemIndex = leftChildIndex;
}
If (rightChildIndex <= heapSize & heap [rightChildIndex]> heap [largerItemIndex])
{
LargerItemIndex = rightChildIndex;
}
If (index! = LargerItemIndex)
{
Swap (heap, index, largerItemIndex );
MaxHeapfy (heap, largerItemIndex );
}
}
Static void Swap (int [] heap, int index1, int index2)
{
Var temp = heap [index1];
Heap [index1] = heap [index2];
Heap [index2] = temp;
}
}
}

1. MaxHeapfy: the premise of this method is that the left and right subtree of the node at the index is already the largest heap, and the ultimate goal is to make the heap with the node at the index as the root become the largest heap.

2. buildMaxHeap: This method involves the fact that if a pair contains n elements, the elements starting from the beginning (assume that the following table of the node starts from 1) it must be a leaf node (this can be proved by the reverse identification method. If the node is not a leaf node, the node must contain subnodes, therefore, we can draw the conclusion that the index of the left child is 2 * ()> n, which is obviously incorrect ). On this premise, this method is used to build the maximum heap through MaxHeapfy at the bottom.

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.