Created
October 10, 2012 00:44
-
-
Save scottchiang/3862462 to your computer and use it in GitHub Desktop.
Solution for Linear Search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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