Method: Algorithms::Sort.heapsort

Defined in:
lib/algorithms/sort.rb

.heapsort(container) ⇒ Object

Heap sort: Uses a heap (implemented by the Containers module) to sort the collection. Requirements: Needs to be able to compare elements with <=> Time Complexity: О(n^2) Space Complexity: О(n) total, O(1) auxiliary Stable: Yes

Algorithms::Sort.heapsort [5, 4, 3, 1, 2] => [1, 2, 3, 4, 5]


85
86
87
88
89
90
# File 'lib/algorithms/sort.rb', line 85

def self.heapsort(container)
  heap = Containers::Heap.new(container)
  ary = []
  ary << heap.pop until heap.empty?
  ary
end