Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Heapsort
(section)
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)''
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)