Saturday, July 18, 2015

Python: binary search (3)


Abstract: hunt a value in a given list.

The loop is used for  binary search, instead of the method of recursion previously used. Additionally, duplicates is allowed in the list.

The result:
Search 1:
search the index of 5
0 12 24
    0 12
    1 5
    The index is [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]

Search 2:
search the index of 23
0 12 24
    12 24
    5 34
12 18 24
    18 24
    6 34
18 21 24
    21 24
    9 34
21 22 24
    22 24
    10 34
22 23 24
    23 24
    12 34
    The index of boundary is (23, 24)
    The boundary is 12 - 34

Search 3:
search the index of 1
0 12 24
    0 12
    1 5
    The index is [0]

Search 4:
search the index of 17450000
    not in this list

Search 5:
search the index of -24
    not in this list


The script:
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 18 10:27:07 2015

@author: yuan
"""
import math
#implement loop instead of recursion
#hunt the approximate values of a given value using binary search
class search:
   
    def __init__(self, arr):
        self.arr=arr
   
    def binary_search(self,start, end, target):
        limit_end=end
        #out of bounds       
        if target<self.arr[start] or target>self.arr[end]:
            return
           
        #loops
        while end-start>1:
            mid=int(math.ceil((end-start)/2)) +start
            print start, mid, end
      
            if target<=self.arr[mid]:
                end=mid
            else:
                start=mid
            print '\t', start, end
            print '\t', self.arr[start], self.arr[end] 
            #
            if target==self.arr[start]:
                self.index.append(start)
                self.boundary=(start-1,start+1)
                break
            elif target==self.arr[end]:
                self.boundary=(end-1,end+1)
                self.index.append(end)
                break
        #duplicates
        if len(self.index)>0:
            while self.index[0]>0 and self.arr[self.index[0]]==self.arr[(self.index[0]-1)]:
                self.index.insert(0,(self.index[0]-1))
            while self.index[-1]<limit_end and self.arr[self.index[-1]]==self.arr[(self.index[-1]+1)]:
                self.index.append((self.index[-1]+1))
        #boundary
        self.boundary=(start,end)
        return
       
    def enter_search(self, target):
        self.index=[]
        self.boundary=()
        print 'search the index of', target
        boundary=self.binary_search(0, len(self.arr)-1, target)
        #
        if len(self.index)>0:
            print '\tThe index is', self.index
        elif len(self.boundary)>0:
            print '\tThe index of boundary is',self.boundary
            print '\tThe boundary is',self.arr[self.boundary[0]], '-', self.arr[self.boundary[1]]
        else:
            print '\tnot in this list'
           
#main program
arr=[1,2,3,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,7,8,9,10,12,34]
b=search(arr)

print 'Search 1:'
target=5
b.enter_search(target)

print '\nSearch 2:'
target=23
b.enter_search(target)

print '\nSearch 3:'
target=1
b.enter_search(target)

print '\nSearch 4:'
target=17450000
b.enter_search(target)

print '\nSearch 5:'
target=-24
b.enter_search(target)


No comments:

Post a Comment