[2] algorithm path-heap sorting of Selection

Source: Internet
Author: User

Question:

The concept of sorting method is simple. A minimum value is selected for the sorting part and the backend of the sorted part is inserted. The time spent mainly searching for the minimum value for the whole unsorted part, if you can quickly search for the minimum value, you can select a sort method to speed up the search.

The heap sorting method allows the search path from the root to the last leaf, instead of the entire unordered part, which can accelerate the sorting process. Therefore, it is called the improved selection sorting method.


The whole heap sorting process is divided into three processes: heap creation, value, and adjustment to the new heap. The following figure shows the minimum stacked tree. For more information about heaptree, see data structures and algorithms)

Heap building-Algorithm

1. elements added to the stacked tree are first placed at the last leaf node.

2. Check whether the parent node is smaller than the child node (minimum accumulation)

3. Swap small elements with the parent node until the accumulation tree conditions are met.


Minimum value-Algorithm

1. Swap the root node with the last leaf node

2. Reduce the tree length by one and adjust the tree to a new stacked tree.


Adjusted to new stacked tree-Algorithm

1. Compare the left and right child nodes to a smaller node.

2. Compare the child node and parent node. If the parent node is large, the child node is exchanged with the parent node, and the child node is the new parent node, and the new child node is requested. The program enters 1. Loop


Sourcecodes


// Create the minimum accumulation tree-heap creation algorithm // 1. add the stacked element to the position of the last leaf node first // 2. check whether the parent node is smaller than the child node. If it is small, the parent node and the child node are exchanged. // 3. set the parent node to a new child node, and request its new parent node. The program enters. loop int creatminheap3 (int A [], int lens) {int I; // temporary incremental variable int child, parent; // subnode and parent node subscript int * pheap = new int [lens]; // new heap for (I = 1; I <lens; I ++) {pheap [I] = A [I]; child = I; parent = Child/2; while (Child> = 2 & pheap [Parent]> pheap [child]) {swaper (pheap [Parent], pheap [child]); child = parent; parent = Child/2 ;}for (I = 1; I <lens; I ++) {A [I] = pheap [I];} Delete pheap; pheap = NULL; return 0 ;}
 


// Adjust the minimum accumulation tree // 1. compare the left and right child nodes, and select a smaller node. // 2. compare the child node and parent node. If the parent node is large, the child node is exchanged with the parent node, and the child node is the new parent node. Then, the program enters 1. loop int adjustminheap2 (int A [], int specificlens) {If (specificlens <2) Return-1; if (specificlens = 2) {if (a [1]> A [2]) swaper (A [1], a [2]); Return 0 ;}int tail = specificlens; int parent = 1; int child = 2 * parent; while (Child + 1 <= tail) {if (a [child] <A [Child + 1]) {if (a [Parent]> A [child]) {swaper (A [Parent], a [child]); parent = Child; child = 2 * parent ;} else break;} else {if (a [Parent]> A [Child + 1]) {swaper (A [Parent], a [Child + 1]); parent = Child + 1; child = 2 * parent;} else break;} return 0 ;}



// Heap sorting // 1. create a minimum stacked tree // 2. remove the smallest node (swap the root of the smallest stacked tree and the last node) // 3. the tree length is reduced by one, and the new heap is adjusted to the minimum heap tree. // 4. program to enter. 2 loop int heapsort (int A [], int lens) {creatminheap3 (A, lens); int I = 0; int parent, child; int M = Lens-1; while (M> 1) {swaper (A [1], a [m]); m --; adjustminheap2 (a, m);} return 0 ;}




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.