class Searches

Searches class 4 methods corresponding to algorithm_selector module

Public Instance Methods

all(data_set, trials, target) click to toggle source

Show all data structures for searching

# File lib/algorithm_selector.rb, line 223
def all(data_set, trials, target)
  stack = Stack.new(data_set.length, data_set)
  queue = Queue.new(data_set.length, data_set)
  linked_list = LinkedList.new(data_set)
  binary_tree = BinaryTree.new(data_set)
  {
    found: stack.search(target),
    results:
      {
        stack: display_time(Proc.new {stack.search(target)}, trials),
        queue: display_time(Proc.new {queue.search(target)}, trials),
        linked_list: display_time(Proc.new {linked_list.search(target)}, trials),
        binary_tree: display_time(Proc.new {binary_tree.search(target)}, trials)
      }
  }
end
analyze(data_set, algorithm, trials, target) click to toggle source

Show analysis of a data structure for searching

# File lib/algorithm_selector.rb, line 257
def analyze(data_set, algorithm, trials, target)
  hash = all(data_set, trials, target)
  alg = {}
  hash[:results].each do |key, val|
    if algorithm.to_sym == key
      alg = Hash[key, val]
    end
  end
  {
    found: hash[:found],
    alg.keys[0] => alg[alg.keys[0]]
  }
end
best(data_set, trials, target) click to toggle source

Show best data structure for searching

# File lib/algorithm_selector.rb, line 241
def best(data_set, trials, target)
  hash = all(data_set, trials, target)
  best_time = 0
  best_algorithm = {}
  hash[:results].each do |key, val|
    if val < best_time || best_time == 0
      best_time = val
      best_algorithm = Hash[key, val]
    end
  end
  {
    best_algorithm.keys[0] => best_algorithm[best_algorithm.keys[0]]
  }
end
compare(data_set, first_algorithm, second_algorithm, trials, target) click to toggle source

Show comparison of two data structures for searching

# File lib/algorithm_selector.rb, line 272
def compare(data_set, first_algorithm, second_algorithm, trials, target)
  hash = all(data_set, trials, target)
  first = {}
  second = {}
  hash[:results].each do |key, val|
    if first_algorithm.to_sym == key
      first = Hash[key, val]
    elsif second_algorithm.to_sym == key
      second = Hash[key, val]
    end
  end
  {
    first.keys[0] => first[first.keys[0]],
    second.keys[0] => second[second.keys[0]]
  }
end