Class: Searches

Inherits:
Object
  • Object
show all
Defined in:
lib/algorithm_selector.rb

Overview

Searches class 4 methods corresponding to algorithm_selector module

Instance Method Summary collapse

Instance Method Details

#all(data_set, trials, target) ⇒ Object

Show all data structures for searching



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# 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) ⇒ Object

Show analysis of a data structure for searching



257
258
259
260
261
262
263
264
265
266
267
268
269
# 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) ⇒ Object

Show best data structure for searching



241
242
243
244
245
246
247
248
249
250
251
252
253
254
# 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) ⇒ Object

Show comparison of two data structures for searching



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# 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