Class: Algorithmable::Sort::BinaryHeap
- Inherits:
-
Object
- Object
- Algorithmable::Sort::BinaryHeap
- Includes:
- DataStructs
- Defined in:
- lib/algorithmable/sort/binary_heap.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(collection = []) ⇒ BinaryHeap
constructor
A new instance of BinaryHeap.
- #sort ⇒ Object
Methods included from DataStructs
#new_bag, #new_fifo_queue, #new_lifo_queue, #new_max_priority_queue, #new_min_priority_queue, #new_ordered_symbol_table, #new_priority_queue
Methods included from DataStructs::LinkedList
#new_doubly_linked_list, #new_singly_linked_list
Constructor Details
#initialize(collection = []) ⇒ BinaryHeap
Returns a new instance of BinaryHeap.
10 11 12 |
# File 'lib/algorithmable/sort/binary_heap.rb', line 10 def initialize(collection = []) @heap = new_min_priority_queue collection end |
Class Method Details
.sort(collection) ⇒ Object
6 7 8 |
# File 'lib/algorithmable/sort/binary_heap.rb', line 6 def self.sort(collection) new(collection).sort end |
Instance Method Details
#sort ⇒ Object
14 15 16 17 18 |
# File 'lib/algorithmable/sort/binary_heap.rb', line 14 def sort [].tap do |result| result << @heap.dequeue until @heap.empty? end end |