Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #linear search
- def linear_search(looking_for, sequence, sorted):
- index=-1
- for i, val in enumerate(sequence):
- if sorted: #operating on sorted list
- if val>looking_for: #don't bother looking any further
- print(val) #for debug
- break
- if val==looking_for:
- index=i
- break #stop at first match
- return index
- #test
- array=[2,5,4,6,7,4,5,6,8,2]
- sorted_array=[1,2,3,4,5,7,8,9,10] #6 missing for test purposes
- print(linear_search(7,array, False))
- print(linear_search(6,sorted_array, True))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement