Skip to content

Instantly share code, notes, and snippets.

@scottchiang
Created October 10, 2012 00:44
Show Gist options
  • Save scottchiang/3862462 to your computer and use it in GitHub Desktop.
Save scottchiang/3862462 to your computer and use it in GitHub Desktop.
Solution for Linear Search
#linear search
def linear_search(object, array)
index = 0
while array[index] != nil
if object != array[index]
mismatch += 1
end
if object == array[index]
index
break
end
index += 1
end
index
end
#global search
def linear_search(object, array)
index = 0
matched_elements = []
while array[index] != nil
if object != array[index]
mismatch += 1
end
if object == array[index]
matched_elements << index
end
index += 1
end
matched_elements
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment