From d85c08fe677111939101c4d5942e306091a6cbfa Mon Sep 17 00:00:00 2001 From: Keetiim Date: Thu, 21 Nov 2013 15:12:59 -0500 Subject: [PATCH 1/9] Update Winners --- projects/bsearch/Winners | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/projects/bsearch/Winners b/projects/bsearch/Winners index f58e252..7dbb46f 100644 --- a/projects/bsearch/Winners +++ b/projects/bsearch/Winners @@ -1,4 +1,3 @@ - def bsearch(list,searchvalue): low=0 @@ -49,3 +48,19 @@ def bsearch(List,item):#defines the function for binary search of an item in a l bottom=mid+1 #assigns the index of the element succeeding the mid to bottom else: return -1 #returns -1 if the search item is not found +#------------------------------------------------------------------------------ + +# Ketema Desissa +# @02594803 +def bsearch(N,target): + Low=0 + High=len(N)-1 + while(Low<=High): + mid=(Low+High)/2 + if(N[mid]==target): + return mid + elif(N[mid]target): + High=mid-1 + return -1 From 071add9de2ad80510ee44ae6b514ed376a9287ed Mon Sep 17 00:00:00 2001 From: TewodrosB Date: Fri, 22 Nov 2013 10:15:49 -0500 Subject: [PATCH 2/9] update winners bsearch file --- projects/bsearch/Winners | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/projects/bsearch/Winners b/projects/bsearch/Winners index f58e252..3edf59a 100644 --- a/projects/bsearch/Winners +++ b/projects/bsearch/Winners @@ -1,4 +1,3 @@ - def bsearch(list,searchvalue): low=0 @@ -49,3 +48,25 @@ def bsearch(List,item):#defines the function for binary search of an item in a l bottom=mid+1 #assigns the index of the element succeeding the mid to bottom else: return -1 #returns -1 if the search item is not found + + +#------------------------------------------------------------------------------------------------------- + +# Tewodros Bejiga @02618567 + +def bsearch(list,target): + low = 0 # low is the first index of the list + high = len(list) -1 # high is the highest index in the list which is one less than the lenght of the list. + while low <= high: + mid =(low + high)/2 # mid is the middle element of the list + if target == list[mid]: # In binary search the first element in the list we check to compare with the element we looking for is the middle element + return mid # if the target element is exactlly equal to the middle element then the code will return the index of the element. + elif target < list[mid]: # But in a situation when the target element is less than the middle element then we will search the target in the lower half of the list. + high = mid - 1 + elif target > list[mid]: # when the target element is greater than the middle element then we will search the element in the upper half of the list. + low = mid + 1 + else: + return -1 # Or if the target element is not in the list the code will return -1 in response to that + + + From 2c8bc2b28cf7907c2433d696c75c203d0a821223 Mon Sep 17 00:00:00 2001 From: vforeman Date: Fri, 22 Nov 2013 10:20:32 -0500 Subject: [PATCH 3/9] Update Winners --- projects/bsearch/Winners | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/projects/bsearch/Winners b/projects/bsearch/Winners index 3edf59a..396c6e1 100644 --- a/projects/bsearch/Winners +++ b/projects/bsearch/Winners @@ -70,3 +70,41 @@ def bsearch(list,target): + +def bsearch(pList,pFind): #the function's arguments are a list and an element to be found + + pList.sort() #first the list is sorted using the list class method sort() + + low=0 #the minimum value of the search range is initialied at 0 + high=len(pList)-1 #and the max value is initialized at the last position in the list + + while(low<=high): #the search will run up to and including a range of 1 element. + #If the search element is not in the list at all then the max and + #min value will be equal to the mean position and either the max or min + #value will be assigned the position preceeding or suceeding the mean, respectively, + #and the condition (low<=high) will be false and the loop will terminate + + mid=(low+high)/2 #at each iteration, the mean position of the range is found + + if(pList[mid]==pFind): #if the search element is equal to the element at the mean position + return mid #then the mean position is returned + + elif(pList[mid]pFind): #the last case handled is if the search element is smaller than the + high=mid-1 #element at the mean position. In this case I reduce the search range by + #assigning the max value the position preceeding the mean position + + return -1 #if the search loop terminates without finding the position of the search + #element then that means the element was not found in the list and -1 is returned + + + + + + From 0998eaf7cb9b45220dd87e4567d54d307a45f240 Mon Sep 17 00:00:00 2001 From: vforeman Date: Fri, 22 Nov 2013 10:34:32 -0500 Subject: [PATCH 4/9] Update Winners --- projects/bsearch/Winners | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/projects/bsearch/Winners b/projects/bsearch/Winners index 396c6e1..f956135 100644 --- a/projects/bsearch/Winners +++ b/projects/bsearch/Winners @@ -48,8 +48,25 @@ def bsearch(List,item):#defines the function for binary search of an item in a l bottom=mid+1 #assigns the index of the element succeeding the mid to bottom else: return -1 #returns -1 if the search item is not found - - +#------------------------------------------------------------------------------------------------------- + +# Ketema Desissa +# @02594803 +def bsearch(N,target): + Low=0 + High=len(N)-1 + while(Low<=High): + mid=(Low+High)/2 + if(N[mid]==target): + return mid + elif(N[mid]target): + High=mid-1 + return -1 + + + #------------------------------------------------------------------------------------------------------- # Tewodros Bejiga @02618567 From e915f5c5eaa3193aa4668cf6cd5599508adc43a2 Mon Sep 17 00:00:00 2001 From: vforeman Date: Fri, 22 Nov 2013 11:32:34 -0500 Subject: [PATCH 5/9] Update Winners deleted ketema's code i added in order to merge with commands. --- projects/bsearch/Winners | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/projects/bsearch/Winners b/projects/bsearch/Winners index f956135..cf5c031 100644 --- a/projects/bsearch/Winners +++ b/projects/bsearch/Winners @@ -48,26 +48,7 @@ def bsearch(List,item):#defines the function for binary search of an item in a l bottom=mid+1 #assigns the index of the element succeeding the mid to bottom else: return -1 #returns -1 if the search item is not found -#------------------------------------------------------------------------------------------------------- -# Ketema Desissa -# @02594803 -def bsearch(N,target): - Low=0 - High=len(N)-1 - while(Low<=High): - mid=(Low+High)/2 - if(N[mid]==target): - return mid - elif(N[mid]target): - High=mid-1 - return -1 - - - -#------------------------------------------------------------------------------------------------------- # Tewodros Bejiga @02618567 From c43d1f213c5c5d3e3540b80d7777d093864f21f8 Mon Sep 17 00:00:00 2001 From: vforeman Date: Fri, 22 Nov 2013 12:02:37 -0500 Subject: [PATCH 6/9] 1 line deletion I removed the "<<<<<< Date: Fri, 22 Nov 2013 20:08:49 -0500 Subject: [PATCH 7/9] Update Winners --- projects/bsearch/Winners | 1 + 1 file changed, 1 insertion(+) diff --git a/projects/bsearch/Winners b/projects/bsearch/Winners index 5b60b43..00bec50 100644 --- a/projects/bsearch/Winners +++ b/projects/bsearch/Winners @@ -1,3 +1,4 @@ + def bsearch(list,searchvalue): low=0 From 38d7d2c8f6697f4b8658d65161b5dc6df7d2ca23 Mon Sep 17 00:00:00 2001 From: vforeman Date: Fri, 22 Nov 2013 20:12:00 -0500 Subject: [PATCH 8/9] Update Winners --- projects/bsearch/Winners | 23 ----------------------- 1 file changed, 23 deletions(-) diff --git a/projects/bsearch/Winners b/projects/bsearch/Winners index 00bec50..1c69a1e 100644 --- a/projects/bsearch/Winners +++ b/projects/bsearch/Winners @@ -1,4 +1,3 @@ - def bsearch(list,searchvalue): low=0 @@ -105,25 +104,3 @@ def bsearch(pList,pFind): #the function's arguments are a list and an elem - - - -#------------------------------------------------------------------------------ - -# Ketema Desissa -# @02594803 -def bsearch(N,target): - Low=0 - High=len(N)-1 - while(Low<=High): - mid=(Low+High)/2 - if(N[mid]==target): - return mid - elif(N[mid]target): - High=mid-1 - return -1 - - - From f8ddbd56c6b2a820dccfdce6e0739948527a8546 Mon Sep 17 00:00:00 2001 From: vforeman Date: Fri, 22 Nov 2013 20:17:28 -0500 Subject: [PATCH 9/9] Update Winners --- projects/bsearch/Winners | 1 + 1 file changed, 1 insertion(+) diff --git a/projects/bsearch/Winners b/projects/bsearch/Winners index 1c69a1e..9f7db44 100644 --- a/projects/bsearch/Winners +++ b/projects/bsearch/Winners @@ -1,3 +1,4 @@ + def bsearch(list,searchvalue): low=0