Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/simms_structures/hashing.rb,
lib/simms_structures/heap_sort.rb

Instance Method Summary collapse

Instance Method Details

#hashObject

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



6
7
8
9
10
11
12
13
# 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!Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# 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