class Array

Public Instance Methods

hash() click to toggle source

this doesn’t type check, but needs the array to be only integer values. XOR and converting to binary produces a unique binary digit, which is then hashed with Ruby’s built in Fixnum#hash

# File lib/simms_structures/hashing.rb, line 6
def hash
  output = []
  each_with_index do |el, idx|
    el = el.hash unless el.is_a?(Numeric)
    output << (el ^ idx).to_s(2).to_i
  end
  output.join.to_i.hash
end
heap_sort!() click to toggle source
# File lib/simms_structures/heap_sort.rb, line 4
def heap_sort!

  0.upto(length - 1) do |idx|
    BinaryMinHeap.heapify_up(self, idx)
  end

  (length - 1).downto(1) do |idx|
    self[0], self[idx] = self[idx], self[0]
    BinaryMinHeap.heapify_down(self, 0, idx )
  end

  reverse!
end