Skip to content

Instantly share code, notes, and snippets.

@neoneo40
Last active August 29, 2015 14:15
Show Gist options
  • Save neoneo40/3309c285730f210b4fa7 to your computer and use it in GitHub Desktop.
Save neoneo40/3309c285730f210b4fa7 to your computer and use it in GitHub Desktop.
Compare Sequential Search & Binary Search
import random
def sequentialSearch(alist, item):
pos = 0
found = False
count = len(alist)
while pos < count and not found:
if alist[pos] == item:
found = True
else:
pos = pos + 1
return found
def sequentialSearch_release(alist, item):
pos = 0
found = False
return alist[0] == item or alist[1] == item or alist[2] == item
def binarySearch(alist, item):
first = 0
last = len(alist) - 1
found = False
while first <= last and not found:
midpoint = (first + last) // 2
if alist[midpoint] == item:
found = True
else:
if item < alist[midpoint]:
last = midpoint - 1
else:
first = midpoint + 1
return found
def create_rand(length):
return [random.randint(0, 100) for i in range(length)]
def rand_init_val(alist):
alist2 = alist[:]
random.shuffle(alist2)
return alist2[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment