Skip to content

Instantly share code, notes, and snippets.

@DustinAlandzes
Created March 6, 2019 03:27
Show Gist options
  • Save DustinAlandzes/a63ae66b5d84c2c4800f646a85cb3aa8 to your computer and use it in GitHub Desktop.
Save DustinAlandzes/a63ae66b5d84c2c4800f646a85cb3aa8 to your computer and use it in GitHub Desktop.
(unorderd and orderd) linear/sequential search implemented with python lists: http://interactivepython.org/runestone/static/pythonds/SortSearch/TheSequentialSearch.html
def orderedSequentialSearch(alist, item):
'''
O(n/2) in item is not present average case
'''
pos = 0
found = False
stop = False
while pos < len(alist) and not found and not stop:
if alist[pos] == item:
found = True
else:
if alist[pos] > item:
stop = True
else:
pos = pos+1
return found
testlist = [0, 1, 2, 8, 13, 17, 19, 32, 42,]
print(orderedSequentialSearch(testlist, 3))
print(orderedSequentialSearch(testlist, 13))
def sequentialSearch(alist, item):
'''
O(n) in item is not present average case
'''
pos = 0
found = False
while pos < len(alist) and not found:
if alist[pos] == item:
found = True
else:
pos = pos+1
return found
testlist = [1, 2, 32, 8, 17, 19, 42, 13, 0]
print(sequentialSearch(testlist, 3))
print(sequentialSearch(testlist, 13))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment