linear search


See On Github

Data

Contributor

Generic placeholder thumbnail

by jay754

in ruby

Tags

linear, search

Source Code

def linear_search(array, value)
    for element in 0..array.length
        if array[element] == value
            return element
        end
    end

    return -1
end
require './linear_search'

describe "linear_search test" do
  it "empty array" do
    linear_search([], 1).should eq(-1)
  end

  it "Search for an element in array" do
    linear_search([1,3,4,10,32], 10).should eq(3)
  end
end