Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Heapsort
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Standard implementation === Although it is convenient to think of the two phases separately, most implementations combine the two, allowing a single instance of {{code|siftDown}}to be [[Inline expansion|expanded inline]].<ref>{{harvnb|Knuth|1997}}</ref>{{rp|Algorithm H}} Two variables (here, {{code|start}} and {{code|end}}) keep track of the bounds of the heap area. The portion of the array before {{code|start}} is unsorted, while the portion beginning at {{code|end}} is sorted. Heap construction decreases {{code|start}} until it is zero, after which heap extraction decreases {{code|end}} until it is 1 and the array is entirely sorted. '''procedure''' heapsort(a, count) '''is''' '''input:''' an unordered array ''a'' of length ''count'' start β floor(count/2) end β count '''while''' end > 1 '''do''' '''if''' start > 0 '''then''' ''(Heap construction)'' start β start β 1 '''else''' ''(Heap extraction)'' end β end β 1 swap(a[end], a[0]) ''(The following is siftDown(a, start, end))'' root β start '''while''' iLeftChild(root) < end '''do''' child β iLeftChild(root) ''(If there is a right child and that child is greater)'' '''if''' child+1 < end '''and''' a[child] < a[child+1] '''then''' child β child + 1 '''if''' a[root] < a[child] '''then''' swap(a[root], a[child]) root β child ''(repeat to continue sifting down the child now)'' '''else''' '''break''' ''(return to outer loop)''
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Heapsort
(section)
Add topic