Class: Algorithmable::Sort::BinaryHeap

Inherits:
Object
  • Object
show all
Includes:
DataStructs
Defined in:
lib/algorithmable/sort/binary_heap.rb

Class Method Summary collapse

Instance Method Summary collapse

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

#sortObject



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