#69 Python Tutorial for Beginners | Binary Search Using Python

///#69 Python Tutorial for Beginners | Binary Search Using Python

#69 Python Tutorial for Beginners | Binary Search Using Python

FavoriteLoadingAdd to favorites

Python Tutorial to learn Python programming with examples
Complete Python Tutorial for Beginners Playlist :
Python Tutorial in Hindi :

Github :-

Editing Monitors :

Check out our website:

Follow Telusko on Twitter:

Follow on Facebook:
Telusko :
Navin Reddy :

Follow Navin Reddy on Instagram:

Subscribe to our other channel:
Navin Reddy :
Telusko Hindi :

Donation:
PayPal Id : navinreddy20
Patreon : navinreddy20

source

By |2020-04-28T03:16:44+00:00April 28th, 2020|Python Video Tutorials|38 Comments

38 Comments

  1. safe lab April 28, 2020 at 3:16 am - Reply

    Bala

  2. Abirami Vijay April 28, 2020 at 3:16 am - Reply

    why so many cuts?

  3. Ravi Kumar April 28, 2020 at 3:16 am - Reply

    when I try to print the index number of search value, it is giving me the index number after sorting. what should I do to get it according to the original list

  4. Anirudd Wakikar April 28, 2020 at 3:16 am - Reply

    Why the error list bound is not callable comming..pls comment sir..

  5. Marcin R April 28, 2020 at 3:16 am - Reply

    super hard to watch in 480p … can you make fonts bigger in future videos (this comment make no sense in 2020 :))

  6. MD Bhardwaj April 28, 2020 at 3:16 am - Reply

    it doesn't work perfectly if numbers are repeated.

  7. amit perane April 28, 2020 at 3:16 am - Reply

    Can anyone post this code with
    for loop… Please help…🙏

  8. Mr Elyas April 28, 2020 at 3:16 am - Reply

    I have done the binary search before watching your method:
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    from time import sleep

    list = [4,6,7,55,49,13,46,46,13,48,21,1,8,94]

    list = sorted(list)

    l_val = list.index(min(list))

    u_val = list.index(max(list))

    b = int(input('enter the number for search in the list: '))

    print('searching…')

    sleep(1)

    for a in range(u_val):

    c = (l_val+u_val)//2

    e = list[c]

    if e == b:

    print('the number is found in the list:',b)

    break

    elif e < b:

    l_val = c

    pass

    else:

    u_val = c

    pass

  9. Higgs Boson April 28, 2020 at 3:16 am - Reply

    Thanks sir

  10. Fahad Alqahtani April 28, 2020 at 3:16 am - Reply

    To correct the issues:
    list= [4,8,100,150,177,200,201,205,300,350,400]

    n= 400

    poss = 0

    def search(list,n):

    low = 0

    upper = len(list)

    while low<upper-1:

    mid= (low+upper) // 2

    if list[low]==n:

    return True

    if list[mid] == n:

    global poss

    poss = mid

    return True

    else:

    if list[mid]<n:

    low = mid

    else:

    upper = mid

    return False

    if search(list,n):

    print("found it at: ",poss)

    else:

    print("not found!")

  11. Ryan Aquino April 28, 2020 at 3:16 am - Reply

    i got confused on the part of low = mid + 1 and high = mid – 1

  12. MelloW April 28, 2020 at 3:16 am - Reply

    If you know the element you're looking for exsists in the list, and you just want to find the index for it. Is there any benefit to binary searching, or can you simply .index()? For example, I have a list of dates in the format '2020-03-01' and I want to find the index of the current days date. Is binnary searching more time efficient? Thanks

  13. Akash April 28, 2020 at 3:16 am - Reply

    Sir please make video on binary search using recursion

  14. Blue Guru Digital Marketing April 28, 2020 at 3:16 am - Reply

    Navin, your thought process makes so much sense to me. Thanks for sharing these videos!

  15. Varaprasad Gangapuram April 28, 2020 at 3:16 am - Reply

    I tried this program by converting into for loop but it is not working for starting values because lower bound is increasing so can anybody do this program in for loop and reply to this comment.

  16. Alonso B April 28, 2020 at 3:16 am - Reply

    What's the complexity of the algorithm?

  17. Sandeep Jain April 28, 2020 at 3:16 am - Reply

    Can you tell how many total iterations to be done for finding number 45 ??
    I expected 3 iterations?
    Please reply

  18. Jazab C. April 28, 2020 at 3:16 am - Reply

    why not make the min the mid + 1?

  19. Sunita Tanwar April 28, 2020 at 3:16 am - Reply

    Sir your all video list is good and well arranged but please try to make background font big so that we can see them easily in our Android phones

  20. Ghostmak Gaming and Tech April 28, 2020 at 3:16 am - Reply

    sir it says error in if search(list, n):

  21. Venkateswarlu Guntaka April 28, 2020 at 3:16 am - Reply

  22. tarun tosh April 28, 2020 at 3:16 am - Reply

    This video is very good. Thanks

  23. Poorvi Sharma April 28, 2020 at 3:16 am - Reply

    Hey, how do I implement binary search in a class?

  24. Harman Randhawa April 28, 2020 at 3:16 am - Reply

    not working for last element😑

  25. #Solo 99 April 28, 2020 at 3:16 am - Reply

    pas = 0

    def Binary_search(list,n):

    l = 0

    u = len(list)

    while l <= u:

    mid = (l+u)//2

    if list[mid] == n:

    globals()['pas'] = mid

    return True

    else:

    if list[mid] < n:

    l = mid + 1

    else:

    u = mid – 1

    return False

    list = [1,2,3,7,9,12,56,100,230,566,767]

    n = 767

    Binary_search(list,n)

    if Binary_search(list,n):

    print("found at",pas + 1)

    else:

    print("not found")

  26. Karthick Sambasivam April 28, 2020 at 3:16 am - Reply

    good explanation. one thing pl little bit slow. This is like after a tututorial u r going to catch a train.

  27. Shii Li April 28, 2020 at 3:16 am - Reply

    excuse me, why should "u" and "l" be assigned to "mid +1" and "mid-1" in else block?
    istead of just "mid"

  28. aries sulapas April 28, 2020 at 3:16 am - Reply

    sir can you make it ?
    Create a random list of numbers between 0 and 100 with a difference of 2 between

    each number. Ask the user for a number between 0 and 100 to check whether their

    number is on the list. The program should work like this. The program will half the list of

    numbers and see whether the user’s number matches the middle element in the list. If

    they do not match, the program will check which half the number lies in, and eliminate

    the other half. The search then continues on the remaining half, again checking whether

    the middle element in that half is equal to the user’s number. This process keeps on

    going until the program finds the users number, or until the size of the subarray is 0,

    which means the user’s number isn’t on the list.

  29. PST SAI GANESH April 28, 2020 at 3:16 am - Reply

    Sir you gave this code sorted list but not for unsorted list
    I kindly request you to upload the code for unsorted list

  30. Anand kumar April 28, 2020 at 3:16 am - Reply

    hi Navin sir, Thank you for these amazing videos their is one minor mistake
    so the correct solution of binary search is :
    pos = -1

    def search(list, key):

    l = 0

    h = len(list)-1

    for i in range(len(list)):

    mid = (l + h) // 2

    if list[mid] == key:

    globals()['pos'] = mid

    return True

    else:

    if list[mid] < key:

    l = mid + 1

    else:

    h = mid – 1

    return False

    list = [3, 6, 8, 12, 14, 17, 25, 29, 31, 36, 42, 47, 53, 55, 62]

    key = 62

    if search(list, key):

    print("Element founded at the position ", pos)

    else:

    print("Element is not found")

    o/p:Element founded at the position 14
    and

    pos = -1

    def search(list, key):

    l = 0

    h = len(list)-1

    for i in range(len(list)):

    mid = (l + h) // 2

    if list[mid] == key:

    globals()['pos'] = mid

    return True

    else:

    if list[mid] < key:

    l = mid + 1

    else:

    h = mid – 1

    return False

    list = [3, 6, 8, 12, 14, 17, 25, 29, 31, 36, 42, 47, 53, 55, 62]

    key = 1

    if search(list, key):

    print("Element founded at the position ", pos)

    else:

    print("Element is not found")

    o/p: Element is not found
    and
    def search(list, key):

    l = 0

    h = len(list)-1

    for i in range(len(list)):

    mid = (l + h) // 2

    if list[mid] == key:

    globals()['pos'] = mid

    return True

    else:

    if list[mid] < key:

    l = mid + 1

    else:

    h = mid – 1

    return False

    list = [3, 6, 8, 12, 14, 17, 25, 29, 31, 36, 42, 47, 53, 55, 62]

    key = 6666

    if search(list, key):

    print("Element founded at the position ", pos)

    else:

    print("Element is not found")

    o/p:Element is not found

  31. MESS MASTI April 28, 2020 at 3:16 am - Reply

    a=[4,7,8,12,45,99]

    n=45

    l=0

    u=len(a)-1

    while l<=u:

    m=l+u//2

    if a[m]==n:

    print('found at '+str(m))

    break

    else:

    if n>a[m]:

    l=m

    else:

    u=m

  32. Hussain Qadri April 28, 2020 at 3:16 am - Reply

    Can someone explain why pos = -1

  33. shaik sohail April 28, 2020 at 3:16 am - Reply

    it is printing upto 7 index value..when iam trying to search 8 index value it is not printing anything.The loop is repeating infinity times and still searching

  34. toalopez April 28, 2020 at 3:16 am - Reply

    Sir sorry to correct you, but this is totally wrong binrary search, it is not good, try it using this list of numbers, and search for 92 =
    [4, 7, 8, 12, 24, 43, 48, 68, 79, 89, 92, 130, 150, 162] it returns False, and it is in the list

  35. tahira khan April 28, 2020 at 3:16 am - Reply

    It's not working for me at all

  36. rafi md April 28, 2020 at 3:16 am - Reply

    Hi Naveen…!

    For binary search there is one condition. You have to mention that.

    "Elements should be sorted"

  37. Akshay Katta April 28, 2020 at 3:16 am - Reply

    # Binary Search

    class ABC:

    def __init__(self, list, num):
    self.list = list
    self.num = num

    def search(self):
    l = 0
    u = len(self.list) – 1

    while l<=u:
    mid = (l + u) // 2
    if(self.list[mid] == self.num):
    globals()['pos'] = mid
    return True
    else:
    if self.list[mid] < self.num:
    l = mid+1
    else:
    u = mid-1
    return False

    list1 = []
    pos = -1
    number = int(input("Enter Number of Elements you want: "))
    for i in range(number):
    print("Enter", (i+1), "element:", end="")
    list1.append(int(input()))

    print("List is:", end="")
    for i in list1:
    print(i,end=" ")

    search_key = int(input("nEnter Element to search: "))

    a1 = ABC(list1, search_key)

    if a1.search():
    print("Element Found at:", pos+1)
    else:
    print("Element Not Found!")

  38. Itishree Behera April 28, 2020 at 3:16 am - Reply

    Even if..we write l=mid ..then why it's not showing "not found"..🤔 10is still not there in d list.and l=mid . . that's not d issue for not showing d output.

Leave A Comment

*