Heap sequencing program and proof

Source: Internet
Author: User

 

   

Heap sort: two fork tree. If you arrange in ascending order, the value of the parent node is guaranteed to be less than or equal to the child node.

Heap sequencing Proof:

1.

For I=N/2 Downto 1 do

Down (i);

I. Ensure that all nodes of a subtree with a child node of point I as the root node meet its value less than or equal to the value of its child nodes (if there are child nodes ).

II. Each point I down is guaranteed that all nodes of a subtree with point i as the root node meet its value less than or equal to the value of its child nodes (if there are child nodes ).

Prove:

In the Down (i) operation, the value of point I will be modified to satisfy the value of "its value is less than or equal to its child node (if there is a child node )"; for two numbers of any interchange value (child nodes and parent nodes ), the modification satisfies the " The value of the parent node is less than or equal to the value of its child nodes (if there are child nodes ) ", whereas for other non-commutative orders, the value is less than or equal to the value of its child nodes (if there are child nodes )". So the proof.

2. Each time the value of a[1] is removed, place the value of a[n] into a[1] (n is the size of the current heap, that is, the number of tree nodes ), and then the a[1] is down .

Prove:

Root node a[1] can always reach a point through a certain path A[s], set midway through point d1,d2,... , DK, then a[1]<=a[d1]<=a[d2]<= ... <=a[dk]<=a[s], so a[1] is guaranteed to be the minimum value in a two-fork tree.

Then the a[1] down operation, according to i "Each point I down the operation after the guarantee: The point i is the root node of the subtree of all nodes meet its value is less than equal to its child node value (if there are child nodes ). , so that the current a[1] is the minimum value in the current binary tree after the down operation on A[1. Evidence.

1#include <stdio.h>2#include <stdlib.h>3 #defineMAXN 100004 5 Longa[maxn+1],n;6 7 voidDownLongi)8 {9     Longj,temp;Ten      while(1) One     { Aj=i<<1; -         if(j>N) -              Break; the         if(J!=n && a[j]>a[j+1]) -J + +; -         if(a[i]>A[j]) -         { +temp=A[i]; -a[i]=A[j]; +a[j]=temp; A         } at         Else -              Break; -I=J; -     } - } -  in intMain () - { to     Longnn,i; +scanf"%ld",&n); -      for(i=1; i<=n;i++) thescanf"%ld",&a[i]); *      for(I=n >>1; i>=1; i--) $ Down (i);Panax Notoginsengnn=N; -      for(i=1; i<=nn;i++) the     { +printf"%ld", a[1]); Aa[1]=A[n]; then--; +Down1); -     } $     return 0; $}

Heap sequencing program and proof

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.