how can i do a binary search in python?

+1 vote
can you give an example?
Apr 2, 2019 in Python by Waseem
• 4,540 points
103 views

2 answers to this question.

0 votes
def Binary_search (array , element):

        first = 0 

        last = n-1

        found = False

        while first<=last and not found:

                mid = (first+last)//2

                if element > array[mid] :

                    first = mid + 1 

                elif array[mid] == element :

                    found = True

                else :

                    last = mid - 1

        if found == True:

            print ('ELEMENT IS FOUND At LOCATION : ' ,mid)

        else:

            print ('ELEMENT IS NOT FOUND ')



import numpy 

n = int (input ('ENTER ARRAY SIZE \n'))

array = numpy.ndarray(shape = (n) , dtype = int)

print ('ENTER ARRAY ELEMENTS IN INCREASING ORDER |_ONLY_| ')

for i in range (n):

array[i] = int (input ())

    

print ('ARRAY ELEMENTS ARE :\n\t=',array)

element = int (input ('ENTER ELEMENT TO SEARCH IN ARRAY \n'))

Binary_search(array,element)





        
answered Jul 10 by _amrut_
0 votes


def Binary_search (array , element):

        first = 0 

        last = n-1

        found = False

        while first<=last and not found:

                mid = (first+last)//2

                if element > array[mid] :

                    first = mid + 1 

                elif array[mid] == element :

                    found = True

                else :

                    last = mid - 1

        if found == True:

            print ('ELEMENT IS FOUND At LOCATION : ' ,mid)

        else:

            print ('ELEMENT IS NOT FOUND ')

import numpy 

n = int (input ('ENTER ARRAY SIZE \n'))

array = numpy.ndarray(shape = (n) , dtype = int)

print ('ENTER ARRAY ELEMENTS IN INCREASING ORDER |_ONLY_| ')

for i in range (n):

array[i] = int (input ())

    

print ('ARRAY ELEMENTS ARE :\n\t=',array)

element = int (input ('ENTER ELEMENT TO SEARCH IN ARRAY \n'))

Binary_search(array,element)

        

answered Jul 10 by anonymous
• 240 points

Related Questions In Python

0 votes
1 answer
+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,870 points
10,020 views
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
321 views
0 votes
1 answer

How can I convert a list of dictionaries from a CSV into a JSON object in Python?

You could try using the AST module. ...READ MORE

answered Apr 17, 2018 in Python by anonymous
1,988 views
0 votes
2 answers

How do I copy a file in python?

copy a file in python  from shutil ...READ MORE

answered Mar 27, 2019 in Python by rajesh
• 1,230 points
170 views
0 votes
1 answer

How do I copy a file in python?

Use the shutil module. copyfile(src, dst) Copy the contents ...READ MORE

answered Jul 31, 2018 in Python by Priyaj
• 57,700 points
122 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,477 views
0 votes
0 answers
+4 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,800 points
1,108 views