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
1,115 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, 2020 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, 2020 by anonymous
• 240 points

Related Questions In Python

0 votes
1 answer

I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

answered Apr 7, 2020 in Python by Gitika
• 65,770 points
2,418 views
+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,890 points
20,075 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
1,336 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
3,480 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,270 points
1,349 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
• 58,020 points
994 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,464 views
0 votes
1 answer
+5 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,790 points
4,125 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP