Algorithms / Search

require 'date'

def liner_search(lst, x)
    i = 0
    length = lst.size
    while i < length
        return i if lst[i] == x

        i += 1
    end

    return -1
end

items = [2357111317]
puts liner_search(items, 1)   # -1
puts liner_search(items, 7)   # 3
puts liner_search(items, 19)  # -1

# *** simplified speed test ***
items = (0..1000000).to_a
count = 100

start = DateTime.now

count.times{ |i|
    liner_search(items, 777777)
}

delta = DateTime.now - start
seconds = (delta * 24 * 60 * 60).to_f

puts "seconds is #{seconds}"
# about 1.68001 seconds