From 0b8a494b6225d1133c73e666b0300a039bb2b5e3 Mon Sep 17 00:00:00 2001 From: Chetan Kaushik Date: Sat, 23 Jul 2016 17:33:51 +0530 Subject: [PATCH] Added Binary Search and Modified Linear Search --- BinarySeach.py | 29 +++++++++++++++++++++++++++++ LinearSearch.py | 7 ------- 2 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 BinarySeach.py diff --git a/BinarySeach.py b/BinarySeach.py new file mode 100644 index 000000000..b4828c64c --- /dev/null +++ b/BinarySeach.py @@ -0,0 +1,29 @@ +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 + print("Found") + else: + + if item < alist[midpoint]: + + last = midpoint-1 + else: + first = midpoint+1 + if found == False: + + print("Not found") + return found +print("Enter numbers seprated by space") +s = input() +numbers = list(map(int, s.split())) +trgt =int( input('enter a single number to be found in the list ')) +binarySearch(numbers, trgt) + diff --git a/LinearSearch.py b/LinearSearch.py index 12117896c..6180eac5a 100644 --- a/LinearSearch.py +++ b/LinearSearch.py @@ -1,10 +1,3 @@ -def search_linear(x,y): - n = len( x ) - for i in range(n): - if theValue[i] == y: - return True - return false - def sequentialSearch(alist, item): pos = 0 found = False