Last active
August 29, 2015 14:15
-
-
Save neoneo40/3309c285730f210b4fa7 to your computer and use it in GitHub Desktop.
Compare Sequential Search & Binary Search
This file contains hidden or 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
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