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
Priority queue
(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!
=== Naive implementations === One can create a simple, but inefficient priority queue in a number of ways. These naive implementations can demonstrate the expected behaviour of a priority queue in a simpler manner. * ''insert'' elements into an unsorted array; find and ''extract'' element with ''highest priority'' *: Performance: "''insert''" performs in O(1) constant time, where "''extract_max''" performs in O(n) linear time. '''insert'''(element, priority): node.element β element node.priority β priority list.append(node) '''extract_max'''(): highest β 0 foreach node in list: if highest.priority < node.priority: highest β node list.remove(highest) return highest.element * ''insert'' elements into a sorted array; ''extract'' first element *: Performance: "''insert''" performs in O(n) linear time, where "''extract_max''" performs in O(1) constant time. '''insert'''(element, priority): node.element β element node.priority β priority for i in [0...N]: element β list.get_at_index(i) if element.priority < node.priority: list.insert_at_index(node, i + 1) return '''extract_max'''(): highest β list.get_at_index(0) list.remove(highest) return highest.element
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)