Sort algorithm-heap sort

Source: Internet
Author: User

Heap sorting algorithm is based on the heap of this data structure, in fact, the heap listening very high-end, in fact, is very simple, is a binary tree, but also special conditions, is that its parent node is larger than the child nodes (or are small) heap is called the largest heap (minimum heap), instantaneous feeling is very simple, the simplest way to save is
Given a set of numbers, we want to use heap sorting, first of all we need to build a heap, but this group of numbers is certainly not satisfied with the nature of the heap above, so we need to adjust, let him meet the nature of the heap, into a heap, how to adjust it? Take the largest heap, that is, for a node, we determine whether the child has larger than the Father node, there are, exchange these two values, so that the father is bigger than the child, of course, after the exchange may be the child as the root node of the subtree and not satisfied with the nature of the, continue to exchange until satisfied, in order to reduce the number We can start swapping from the node closest to the leaf node, one layer at a to the root node.
Like such a group of numbers a[]={16,7,3,20,17,8}, we build heaps

And then, in turn, adjust

Such a heap was built up. The rest of it is easy.
After the heap we can easily find that the heap has a very good nature, that is, the root node value of the lock all the values are large (small), then we first select the root node, put the array "n-i+1", and then put the value of the "n-i+1" to the root node, adjust the heap, after N times is an orderly. The process is as follows 1

(Pictures are not original, there are copyright issues please contact me to delete)
Heap sorting is categorized into the selection sort, because the idea is the same as the idea of sorting, choosing a maximum (smallest) from the remaining array, placing it in the ordered position, and then continuing to select, just select the order of the complexity is O (n), where the choice is O (1), but the choice is over to adjust, The degree of complexity of the adjustment is log (n), so the total complexity is O (N*LOGN).
In fact, another very common use of the heap is the priority queue, so you can see the complexity of the priority queue, each insertion of an element is O (Logn), take out O (1), so that you can achieve their own priority queue

Code:

#include <iostream>#include <cstdio>#include <vector>using namespace STD;Const intN =1001000;intA[n];voidBalance (intXintN) {intL = x+x;intr = x+x+1;intmid = x;if(L<n && a[l]>a[mid]) mid = l;if(R<n && a[r]>a[mid]) mid = R;if(X!=mid)        {swap (a[x],a[mid]);    Balance (Mid,n); }}voidHeap_sort (intN) { for(inti=n/2-1; i>=0; i--)//Adjust the heap firstBalance (I,n); for(inti=n-1; i>=0; i--) {Swap (a[i],a[0]); Balance (0, i); }}intMain () {//freopen ("Input.txt", "R", stdin);    intN,k; while(~scanf("%d%d", &n,&k)) { for(intI=0; i<n;i++)scanf("%d", &a[i]); Heap_sort (n); for(intI=0; i<n;i++)printf("%d", A[i]);puts(""); }return 0;}

Sort algorithm-heap sort

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.