#69 Python Tutorial for Beginners | Binary Search Using Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 18 ธ.ค. 2024

ความคิดเห็น • 400

  • @ShahzaibElahi
    @ShahzaibElahi 5 ปีที่แล้ว +160

    Remember Binary sort function will be performed on the sorted list, so before executed def binarySeach(list, n): just type list.sort()
    your program will run correctly..

    • @arkamiqbal3221
      @arkamiqbal3221 4 ปีที่แล้ว +2

      bro where to type

    • @mamlzy
      @mamlzy 4 ปีที่แล้ว +3

      @@arkamiqbal3221 u can put below the list

    • @shubhamkokala1992
      @shubhamkokala1992 3 ปีที่แล้ว +9

      This is the most imp step which everyone forgets including me😅

    • @jakkalimahesh7974
      @jakkalimahesh7974 3 ปีที่แล้ว +2

      TQ bro

    • @Ayan8902
      @Ayan8902 3 ปีที่แล้ว +3

      u will not get the actual position of the element if you apply list.sort(). you only find element is present or not

  • @anushkashah9924
    @anushkashah9924 3 ปีที่แล้ว +47

    Great videos sir ! Only I had to point out one correction , I think when we relocate the lowerBound or UpperBound , it should be lowerBound=mid+1 and upperBound=mid-1 .
    You've mentioned it in later part though . For anyone having doubt with the initial part of video , hope this comment helps :p

    • @alokik
      @alokik 3 ปีที่แล้ว +5

      mam, why we have to relocate ?
      and why we are using pos = -1
      it's too confusing.

    • @vigneshm9411
      @vigneshm9411 2 ปีที่แล้ว

      [2,4,3,2,5,6,7,8] plz use this list we will get an error. if you change lower bound= mid-1 then it works

    • @premanshchakraborty4962
      @premanshchakraborty4962 2 ปีที่แล้ว

      @@vigneshm9411 this list is not sorted

    • @mintaowei2686
      @mintaowei2686 ปีที่แล้ว +2

      Exactly. Since the while loop condition is lo

    • @kotaprathyureddy8069
      @kotaprathyureddy8069 ปีที่แล้ว

      I did modifications but i couldn't any output

  • @Dhanush-zj7mf
    @Dhanush-zj7mf 4 ปีที่แล้ว +31

    The best video in this python series. : -)

  • @sidharth1452
    @sidharth1452 3 ปีที่แล้ว +16

    # in binary search the values of an array must be sorted
    """step 1 - set the lowerBound and upperBound
    step 2 - find a min index i.e lowerBound + upperBound / 2
    step 3 - now we have to check that the mid value is same or not with the value we are seaching for
    step 4 - next change your lowerBound or upperBound
    4.1 - the value we are searching for is smaller or bigger than the mid value
    4.1.1 - if your value is smaller change the upperBound to midindex
    4.1.2 - if your value is bigger change the lowerBound to midindex
    step 5 - again find a mid value
    repeat step 4 """
    pos = -1
    def search(arr,n):
    lB = 0
    uB = len(arr)-1
    while lB

    • @Aswini.
      @Aswini. 2 ปีที่แล้ว +2

      Is this working.. when I run In each time I got not found

    • @kotaprathyureddy8069
      @kotaprathyureddy8069 ปีที่แล้ว

      This is code not working broo

    • @sarahpachon9500
      @sarahpachon9500 10 หลายเดือนก่อน

      Thank you its working for me!

    • @sarahpachon9500
      @sarahpachon9500 10 หลายเดือนก่อน

      just change the numbers of the list @@Aswini.

  • @vishakarudhra8665
    @vishakarudhra8665 2 ปีที่แล้ว +9

    Naveen, I haven't found even native english speakers to explain as intuively as you do in this video. Thanks a lot for what you do.

    • @ebisinteidennis
      @ebisinteidennis 2 ปีที่แล้ว

      This is guy is among the legends of programming that have made alot of impacts in our society.

  • @mehulaggarwal7776
    @mehulaggarwal7776 5 ปีที่แล้ว +15

    Finally I understood after your video. Too good 👍

  • @mastertwitter09
    @mastertwitter09 ปีที่แล้ว +17

    Play in 0.75 speed - you're welcome 😁

    • @solomonbestz
      @solomonbestz 10 หลายเดือนก่อน

      ❤❤❤❤ Thanks.......helped alot....man was fast and was making sense

  • @yashwanthaas3233
    @yashwanthaas3233 2 ปีที่แล้ว +1

    Your videos are far better than others

  • @rakeshmanwani4993
    @rakeshmanwani4993 5 ปีที่แล้ว +5

    you are such a good teacher ...

    • @kv_clips
      @kv_clips 5 ปีที่แล้ว

      We are the same

  • @zakiasmaa6834
    @zakiasmaa6834 4 หลายเดือนก่อน

    I want to express my heartfelt gratitude for all the effort you put into simplifying the topics. Your presence and the way you explain things in the videos bring us joy and radiate positive energy. I hope you continue in this way, and I wish you all the best in everything you do.

  • @arjunanselvam
    @arjunanselvam 6 หลายเดือนก่อน

    Excellent. I have been revising python concepts last 2 days by watching your videos and learned many new concepts.

  • @somnvm37
    @somnvm37 3 ปีที่แล้ว +2

    A great video
    Something that helped me with my homework, thought it will take an entire day

  • @prasannakatre4040
    @prasannakatre4040 2 ปีที่แล้ว +3

    Hi Navin sir, my doubt is, the code runs fine for any index position search if we do u = len(list) OR u = len(list) - 1. Why is that?

  • @TG47GRG
    @TG47GRG 6 ปีที่แล้ว +6

    So great explanation. 10/10

  • @afinotantemisan8622
    @afinotantemisan8622 7 หลายเดือนก่อน +1

    Thanks navin for
    You videos are teaching me a lot

  • @akshaybajetha9511
    @akshaybajetha9511 4 ปีที่แล้ว +11

    The code doesn't work when you try to search the last element of the list.
    the corrections are:
    if list[mid] < n:
    l = mid+1
    else:
    u= mid-1

    • @alnesri7109
      @alnesri7109 3 ปีที่แล้ว

      How is it

    • @FIRSTLAST-rl1zn
      @FIRSTLAST-rl1zn 3 ปีที่แล้ว

      Better try
      l = 0
      u = len(list)

    • @manojkbn
      @manojkbn 3 ปีที่แล้ว +1

      @Akshay Thanks, it is working.

    • @dhfm2303
      @dhfm2303 2 ปีที่แล้ว

      he already mentioned at the end

  • @gkof23
    @gkof23 2 ปีที่แล้ว +2

    the shifting of lower and upper bound is so easy to understand, and here I was, trying to slice the list a bunch of times...

  • @daffyduckgamingff
    @daffyduckgamingff 5 หลายเดือนก่อน

    Make sure :
    elif a[mid]

  • @dhruvaggarwal3803
    @dhruvaggarwal3803 4 ปีที่แล้ว

    the best explanation on binary search

  • @sepehrtavakoli591
    @sepehrtavakoli591 ปีที่แล้ว

    You are the best❤❤❤❤❤

  • @aftabhussain1600
    @aftabhussain1600 5 ปีที่แล้ว +14

    Hi Navin, Thank you for these amazing videos.
    However in this algorithm, the last element is not found. Upper bound must be len(list) and not len(list) -1 .
    Another issue was that elements greater than the last value, i.e. elements not present in list and > last list value were not coming being found. This was cause the Lower bound value must be changed to "Mid + 1" instead of Mid.
    Thank you once again for these tutorials.

    • @shiven3020
      @shiven3020 5 ปีที่แล้ว +1

      Exactly my thoughts. Lower = mid +1 or upper = mid - 1, depending on the condition.

    • @bhavyachampaneri
      @bhavyachampaneri 5 ปีที่แล้ว +1

      Yes you are right

    • @rammckee6319
      @rammckee6319 5 ปีที่แล้ว

      Thank you sir for mentioning this

    • @arakurd6351
      @arakurd6351 4 ปีที่แล้ว +2

      The upper bound must be len(list)-1 because the index for the last value of the list is one less than the length of the list because computers start counting from 0. The length of the list itself is an index that does not exist in the list.

    • @studies3943
      @studies3943 2 ปีที่แล้ว

      @@arakurd6351 True, this comment made me confused lol

  • @Mrallrounderr
    @Mrallrounderr 6 ปีที่แล้ว +27

    Sir it's a request plz unload video on Python regularly

  • @RebeccaHamilton-m9y
    @RebeccaHamilton-m9y ปีที่แล้ว

    1:27 list explanation
    4:53 implementation

  • @Nana-dt3pl
    @Nana-dt3pl 5 ปีที่แล้ว +1

    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.

  • @dev5539
    @dev5539 3 ปีที่แล้ว +1

    List = int(input('enter list:'))
    n =int(input('enter no to be searched:'))
    For I in list:
    If i ==n :
    Print ('found')
    else:
    Print ('not found')
    Break
    Print ('thank you')

    • @versatile7713
      @versatile7713 2 ปีที่แล้ว

      But its time complexity is O(n)

  • @iamabhik2009
    @iamabhik2009 5 ปีที่แล้ว +3

    If the search value is smaller than list[mid] then change upper bound and (mid - 1) becomes new upper bound value.
    If the search value is bigger than list[mid] then change lower bound and (mid + 1) becomes new lower bound value.

    • @alokik
      @alokik 3 ปีที่แล้ว

      sir,why we have to relocate ?
      and why we are using pos = -1
      it's too confusing.

    • @techwithsourabh7508
      @techwithsourabh7508 2 ปีที่แล้ว

      Correct logic should be like this to assign new mid values
      if list[mid] < n:
      l = mid + 1
      else:
      u = mid - 1

    • @kotaprathyureddy8069
      @kotaprathyureddy8069 ปีที่แล้ว

      ​@@techwithsourabh7508 i did modifications but i did not get any output

  • @adelinewar3222
    @adelinewar3222 4 ปีที่แล้ว +9

    I cannot understand why you have to add and subtract in the last part of the video? If the value is not found using the algorithm then why do we need to add or reduce to mid?

    • @sattvaggrawal1022
      @sattvaggrawal1022 4 ปีที่แล้ว

      Anyone pls reply I am also confused there

    • @gauravbisht9622
      @gauravbisht9622 4 ปีที่แล้ว

      @@sattvaggrawal1022 Well i think that's bcz of while loop u have to increment by 1 when the mid is small than search value and decrement the value by 1 when mid is greater than search value....

    • @sattvaggrawal1022
      @sattvaggrawal1022 4 ปีที่แล้ว

      @@gauravbisht9622 one of the logic can also be that we already checked the mid and it doesnt make sense to recheck it thats why we take mid+1 for l and as we move to the left in case of U so u = mid-1

    • @100ravpatel
      @100ravpatel 4 ปีที่แล้ว +2

      ## in this case you don't have to add
      pos=0
      def search(li,n):
      l=0
      u=len(li)-1
      while l

    • @rahulezdia
      @rahulezdia 4 ปีที่แล้ว +1

      In order to exit the while loop and return false.
      If mid has reached to the last index, l is still equal to u. And we have while condition while l u (exit the while loop and execute FALSE)

  • @tahirsoban5303
    @tahirsoban5303 ปีที่แล้ว

    Excellent tutorial for learning, Thanks sir. some minor corrections are u= len(list) there is no need -1 and u = mid there is no need +1... Rest of code is superb .

  • @satishreddy7922
    @satishreddy7922 3 ปีที่แล้ว

    bro naveen reddy - your explanation superb..!!

  • @chinmaydas4053
    @chinmaydas4053 6 ปีที่แล้ว +2

    Nice topics u r now discussing..

  • @pankajagrawal1163
    @pankajagrawal1163 5 ปีที่แล้ว +11

    hey Naveen, can you make this video again. Since you change the concept at the end of video for changing lower and upper, I think whole algorithm that you taught at starting will change.
    So please correct that algorithm.

    • @shalemrobin6233
      @shalemrobin6233 4 ปีที่แล้ว +2

      Hi,
      Since the middle element !=n so it doesn't make sense to make it lower or upper, so lower is increased and upper is decreased by 1.

    • @sonjuhivaish5481
      @sonjuhivaish5481 4 ปีที่แล้ว

      can u explain plz?

  • @noumanfaika6364
    @noumanfaika6364 4 ปีที่แล้ว

    One of biggest problem is you never zoom while writing code you should understand ... Becoz visibility is must for learners.. People appreciate by way you present it... Any ways ...nothing can change now

  • @shahmeerahmed3602
    @shahmeerahmed3602 3 ปีที่แล้ว +1

    You helped me alottttttt!!!!❤️❤️❤️

  • @agnesrusaliya6627
    @agnesrusaliya6627 4 ปีที่แล้ว +2

    Thank you so much dude ✨❣️ ! Your teaching is tons better than my professor

  • @Softwareengineeringdev
    @Softwareengineeringdev 6 ปีที่แล้ว +3

    Cool! Liked before watching!

  • @nitinkumar-cx2ew
    @nitinkumar-cx2ew 5 ปีที่แล้ว

    using for loop
    pos=0
    def search(list,n):
    l=0
    u=len(list)-1
    for i in range(u):
    if l

  • @blueguru
    @blueguru 4 ปีที่แล้ว +2

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

  • @kajekikej
    @kajekikej 3 ปีที่แล้ว +1

    Thank you so much for this tutorial.

  • @danielkamau8436
    @danielkamau8436 3 ปีที่แล้ว +1

    def binary_search(nums, num):
    begin_index = 0
    end_index = len(nums) - 1
    while begin_index mid_point_value
    begin_index = mid_point + 1
    return None
    nums = [2,4,5,6,7,8,9,10]
    num = 6
    print(binary_search(nums, num))

    • @saber.x4761
      @saber.x4761 23 วันที่ผ่านมา

      God bless you my nigga

  • @Cop705
    @Cop705 4 ปีที่แล้ว +1

    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

  • @saswataacharya5431
    @saswataacharya5431 4 ปีที่แล้ว

    Using for loop:
    list=[3,4,5,6,7,8,9,70]
    n=9
    pos=0
    def search(list,n):
    l=0
    u=len(list)-1
    for i in list:
    mid=(l+u)//2
    if list[mid]==n:
    globals()['pos']=mid
    return True
    else:
    if n>list[mid]:
    l=mid
    else:
    u=mid
    return False
    if search(list,n):
    print('found at',pos)
    else:
    print('not found')

  • @tarekamami6021
    @tarekamami6021 4 ปีที่แล้ว

    Another way to do Binary Search :
    def binary_search(sequence, item):
    begin_index = 0
    end_index = len(sequence)- 1
    while begin_index

  • @LUXLEACLOTHINGS_ACCESSORIES
    @LUXLEACLOTHINGS_ACCESSORIES 3 ปีที่แล้ว

    I really appreciate this video. Thanks a lot

  • @wizz_of_king3985
    @wizz_of_king3985 4 ปีที่แล้ว

    got = -1
    def search(nums, n):
    l = 0
    u = len(nums) - 1
    for x in range(len(nums)):
    mid = (l + u) // 2
    if nums[mid] == n:
    globals()['got'] = mid+1
    return True
    else:
    if nums[mid] < n:
    l = mid + 1
    else:
    u = mid - 1
    return False
    ls = [9, 12, 23, 87,89,102,140]
    num = 89
    if search(ls, num):
    print("found", got)
    else:
    print("not found")

  • @yarramneediravindraswamy6804
    @yarramneediravindraswamy6804 2 ปีที่แล้ว

    pos = -1
    def search(list_a,n):
    l = 0
    u = len(list_a)-1

    while l

  • @AGifrom2040
    @AGifrom2040 5 ปีที่แล้ว +1

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

  • @SantoshRaj-hx7rx
    @SantoshRaj-hx7rx 5 ปีที่แล้ว +1

    Tried Using for Loop
    def search(list, x):
    l = 0
    u = len(list)
    for l in range(u):
    mid = (l + u)//2
    if list[mid] == x:
    return mid
    else:
    if list[mid] < x:
    l = mid + 1
    else:
    u = mid - 1
    return -1
    list = [1,4,5,7,9,11,12,34]
    x = 12
    result = search(list, x)
    if result != -1:
    print("Number found at ", result)
    else:
    print("Number not found")

    • @dipankararora2633
      @dipankararora2633 5 ปีที่แล้ว

      Santosh Raj thanks

    • @dipankararora2633
      @dipankararora2633 5 ปีที่แล้ว

      Santosh Raj use for I in range in place of l, otherwise it doesn’t find 1st no in the list... as l increments each time

  • @abharathkannan
    @abharathkannan 6 ปีที่แล้ว +3

    Sir please do video on threads where 2 threads work together and share data

  • @maxon5153
    @maxon5153 4 ปีที่แล้ว

    def binary(arr, key):
    return binarySearch(arr, key, 0, len(arr))
    def binarySearch(arr, key, left, right):
    if leftkey:
    return binarySearch(arr, key, left, middle-1)
    else:
    return binarySearch(arr, key, middle+1, right)
    else:
    return "Not found"
    arr = array([2,3,5,1,2,6])
    arr = sort(arr)
    print(binary(arr, 1))

  • @HappyAnimals3D
    @HappyAnimals3D 6 ปีที่แล้ว +1

    Navin, I love your videos. I had just one question in this video and also in bubble sort and linear search videos. What if you have a list of some values which appear more than once. Also will this procedure ignore any string values inside the list or do we have to implement an exception handling ourselves?

  • @okimeobia
    @okimeobia 4 ปีที่แล้ว +2

    Thanks, this helped a lot.

    • @anuragrai8128
      @anuragrai8128 4 ปีที่แล้ว +1

      Can I ask you why are you learning programming.....? Just curious..

    • @okimeobia
      @okimeobia 4 ปีที่แล้ว +1

      @@anuragrai8128 I like creating things for people to use, simply.

  • @muhammadtahubkhatri2144
    @muhammadtahubkhatri2144 3 ปีที่แล้ว

    a simple way to get the last and the first value guys.
    def binary(arr,k):
    i=0
    u=len(arr)-1
    if arr[u]==k:
    globals()["p"]=u
    return True
    for x in range(len(arr)-1):
    mid=(u+i)//2
    if arr[mid]==k:
    globals()['p']=mid
    return True
    else:
    if arr[mid]

  • @BhuvanSaiTeja007
    @BhuvanSaiTeja007 5 ปีที่แล้ว +1

    Mama nee videos keka, ikkada dhaka ochina ante ardham chesko.

  • @akshayshinde2403
    @akshayshinde2403 5 ปีที่แล้ว

    ur videos make so simple learning python. thanks.

  • @kiranlanghe899
    @kiranlanghe899 5 ปีที่แล้ว +1

    Hi Navin, as you have told in above video that if the search number is greater than the mid value then we will shift the lower to the mid but as i know we will always do the mid+1 for shifting lower.. i got confused that which one is correct.

    • @ashok.marthineni
      @ashok.marthineni 5 ปีที่แล้ว +1

      both are correct(mid+1,mid) but if we take mid it makes more iterations than mid+1

  • @rafimd407
    @rafimd407 5 ปีที่แล้ว +2

    Hi Naveen...!
    For binary search there is one condition. You have to mention that.
    "Elements should be sorted"

  • @mohammedsamiuddin2327
    @mohammedsamiuddin2327 5 ปีที่แล้ว

    sir thanks for making such amazing videos ...................big fan sir

  • @chandrashekara2059
    @chandrashekara2059 ปีที่แล้ว

    The upper bound should be changed to u=mid-1 in the else part of else in while loop. Then this will be correct.
    Since we need to decrease the upper bound to mid -1 when n is < than mid.

  • @JimmyJim13
    @JimmyJim13 4 ปีที่แล้ว +1

    Thank you so much. This really helped me out.

  • @AnujSingh-ez4nx
    @AnujSingh-ez4nx 3 ปีที่แล้ว +1

    I am unable to fetch some values of my list though they are in the list but at run time it shows "not found" ?

  • @moazelsawaf2000
    @moazelsawaf2000 5 ปีที่แล้ว +1

    Great video sir ❤

  • @ROHIT-gv7xk
    @ROHIT-gv7xk 5 ปีที่แล้ว +6

    sir their is one minor mistake in 'u=len(list)-1' so it must be "u=len(list)"

    • @MartialWafo
      @MartialWafo 4 ปีที่แล้ว +1

      @icketypickety ROHIT is right!!! try to use the same program searching for 99 and yo'll run into an infinite loop....

    • @upsc_prep_short_notes
      @upsc_prep_short_notes 4 ปีที่แล้ว

      @@MartialWafo I searched for the same no error, no infinite loop ,, its the right concept. send ur code if not working I will try

  • @anindian4601
    @anindian4601 3 ปีที่แล้ว

    Very Nicely explained bro, thanks a lot, u made it very easy to understand, if u dont explain in the beginning if directly start code then i supposed not get it.

  • @taruntosh7970
    @taruntosh7970 5 ปีที่แล้ว

    This video is very good. Thanks

  • @abubakirmahkamov
    @abubakirmahkamov 3 ปีที่แล้ว

    thank you for lesson you are really the best teacher

  • @sandeeppujare6442
    @sandeeppujare6442 3 ปีที่แล้ว

    One question..
    list = [1, 3, 5, 7, 9]
    n = 9
    How is this algorithm searching this?
    Because this is infinit loop here.
    Don't you think your u should be lenghth of the list instead of minus 1?
    u = len(list)
    def binarySearch(hey, need):
    heyLen = len(hey)
    if heyLen

  • @kabirchawla4325
    @kabirchawla4325 3 ปีที่แล้ว +21

    His habit of using a semicolon😂

  • @ricardocambundo2527
    @ricardocambundo2527 4 ปีที่แล้ว

    Yo this series is just amazing

  • @trollotroll2647
    @trollotroll2647 ปีที่แล้ว

    Sir it not give last value of list is our required search. Plz answer sir

  • @EldhoseKurian2792
    @EldhoseKurian2792 6 หลายเดือนก่อน

    I think it is better to use l=mid+1 and u=mid-1 since we know mid value is not the one we are searching for. so it can be avoided

  • @AbhishekKumar-nz9dn
    @AbhishekKumar-nz9dn 4 ปีที่แล้ว +3

    Sir how can we do this with the help of the method of recursion ?

    • @shaikabdullah3610
      @shaikabdullah3610 4 ปีที่แล้ว

      def binarysearchrec(arr,low,high,n):
      if low x:
      return binarysearchrec(arr,low,mid,n)
      else:
      return binarysearchrec(arr,mid,high,n)
      else:
      return -1
      def binarysearchiter(arr,n):
      low = 0
      high = len(arr) - 1
      mid = 0
      while low

  • @techwithsourabh7508
    @techwithsourabh7508 2 ปีที่แล้ว +1

    Thank you Navin sir for the explanation but I have tried to implement this code which you have shown it will not give correct results on boundary values logic of assigning mid value should be change like this
    if list[mid] < n:
    l = mid + 1
    else:
    u = mid - 1

    • @GPC-bc2yo
      @GPC-bc2yo 6 หลายเดือนก่อน

      why

  • @shalya_ayush
    @shalya_ayush 5 ปีที่แล้ว +1

    Sir this code is unable to find the position of 0th element i.e it is printing that the no. 4 according to your program which is at 0th position in list is" Not found". So.,please suggest some changes.

    • @souravsingh9468
      @souravsingh9468 5 ปีที่แล้ว

      Add another if statement i.e
      If list[0] == n:
      print("found at",0)
      return True

  • @saicharan4669
    @saicharan4669 4 ปีที่แล้ว +1

    Sir at last why are we adding +1 and -1 for the mid .
    My doubt it worked perfectly before adding and subtracting 1 to that

    • @devcreed8175
      @devcreed8175 4 ปีที่แล้ว

      Hello Sai, try running without +1 and -1 and pass the value of n as the last or the first element of array. You will get your answer.

  • @FactsDiscoveryyoutube
    @FactsDiscoveryyoutube 5 ปีที่แล้ว +1

    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

    • @sanyamgarg5547
      @sanyamgarg5547 4 ปีที่แล้ว +1

      why have we done mid+1 and mid-1 please tell

    • @gptest1233
      @gptest1233 4 ปีที่แล้ว

      ​@@sanyamgarg5547 Last modification logic : If we concluded that 'midvalue' is not equal to 'n' , then using midvalue index for either lower bound or upper bound is of no use, hence we need to either look at decremented position i.e midvalueIndex-1 (incase of n < midvalue) or incremented position i.e midvalueIndex+1(incase of n>midvalue)

    • @anitapathania1546
      @anitapathania1546 4 ปีที่แล้ว

      thanks for explaining the logic here

    • @thepresistence5935
      @thepresistence5935 3 ปีที่แล้ว

      why you are mentioning return true and return false, can you please elaborate this

  • @inshalahmed5322
    @inshalahmed5322 3 ปีที่แล้ว

    This is just amazing...

  • @sunitatanwar7866
    @sunitatanwar7866 4 ปีที่แล้ว

    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

  • @naveenadhikari5953
    @naveenadhikari5953 6 ปีที่แล้ว

    thxx very much--------very much fun learning python from you

  • @asuradevan1702
    @asuradevan1702 ปีที่แล้ว

    position (pos=0) can be declared like this also because The In search function we are modifing the pos at iteration

  • @ishikaghai4143
    @ishikaghai4143 5 ปีที่แล้ว

    OSM WRK 👌👌

  • @saravananm2315
    @saravananm2315 4 หลายเดือนก่อน

    Binary Search using for loop:
    pos=-1
    def search(x, n):
    l=0
    u=len(x)-1
    for mid in x:
    mid=(l+u)//2
    if x[mid]==n:
    globals()['pos']=mid
    return True
    else:
    if x[mid]

    • @saravananm2315
      @saravananm2315 4 หลายเดือนก่อน

      we need to assign value from list for n

  • @ultrasamad
    @ultrasamad 6 ปีที่แล้ว

    Elegant explanation...

  • @DilanTech
    @DilanTech 5 ปีที่แล้ว

    I like this active and simple way of teaching,,, anyways thank you sir,,,

  • @_SonalBhainsora
    @_SonalBhainsora 6 ปีที่แล้ว

    sir binary search mei upper bound len(list) hoga tabhi ager hum 99 search krenge toh humee uski position pata chelegi ager mere concept mei koi mistake ho toh please bataiyega

  • @manikantar8614
    @manikantar8614 3 ปีที่แล้ว

    If user doesn't enter in sorting order. Then we need to sort the values right?

  • @fxx20101
    @fxx20101 4 ปีที่แล้ว

    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

  • @shaikansarbasha4169
    @shaikansarbasha4169 6 ปีที่แล้ว

    Excellent sir

  • @montyGator
    @montyGator 3 ปีที่แล้ว +1

    Great explation! But at the part when you run it and says at pos 5 dont you start counting from 0 when indexing? Because if you start at 0 and count to 5 you get 99 not 45.

    • @fashionobsession6793
      @fashionobsession6793 2 ปีที่แล้ว

      Same question also raised by me

    • @et7083
      @et7083 2 ปีที่แล้ว

      @@fashionobsession6793 idk if yall still need it but he left the code as print("Found at", pos+1) so its counting starting from 1 instead,, theres an explanation for why he added 1 in his linear search video !!

  • @rockypanda5648
    @rockypanda5648 6 ปีที่แล้ว +2

    I want to give 1000000 likes on each video.

  • @saichandrareddy9140
    @saichandrareddy9140 6 ปีที่แล้ว +1

    Take every thing from the user:)
    print("BINARY SEARCH:")
    arr1 = []
    n = int(input("size of the array:"))
    for i in range(n):
    x = int(input("enter the input into the array:"))
    arr1.append(x)
    c = sorted(arr1)
    print(c)
    pos = -1
    m = int(input("searching element in the array:"))
    def Binarysearch(c, m):
    l = 0 #lower bound
    u = len(c) - 1 #upper bound
    for k in range(u):
    mid = (l+u)//2
    if c[mid] == m:
    globals()['pos'] = mid
    return True
    else:
    if c[mid] < m:
    l = mid+1
    else:
    u = mid-1
    return False
    if Binarysearch(c, m):
    print("Found here:", pos+1)
    else:
    print("NotFound")

  • @vigneshm9411
    @vigneshm9411 2 ปีที่แล้ว

    if we pass a sorted list to the function like(if sorted(List):) why it is not searching for the element?

  • @farxeennn_n
    @farxeennn_n ปีที่แล้ว

    can you upload video on stack, queue, binary tree.

  • @mr.minded4768
    @mr.minded4768 4 ปีที่แล้ว

    But how we can implement this in string suppose if we have a list of strings and we r also searching for a string so will it work with strings as well.

  • @cleversachin
    @cleversachin 3 ปีที่แล้ว

    it is producing infinite loop with a number not found in list. I guess with this logic it will never go to the last position. Should we keep one more check .. if the item is last element or grater than last .. before entering into the loop??.

  • @hari-el7ig
    @hari-el7ig 5 ปีที่แล้ว +1

    Sir with this logic when i searched for the last element of the list , it was not giving me result and cpu was running high.
    i found u= len(list)-1 needs to be changed u =len(lis)
    ❤u sir

  • @marianpascu8474
    @marianpascu8474 2 ปีที่แล้ว

    Wait, does the return statement end the while loop? Because the condition raised at while always has to be true :/

  • @noname_23232
    @noname_23232 3 ปีที่แล้ว

    What would one's do if you should to find couple numbers in list?

  • @surbhi8599
    @surbhi8599 3 ปีที่แล้ว

    Thank You !

  • @alokdubey4771
    @alokdubey4771 6 ปีที่แล้ว

    Sir can u tell me what should I do for my Ty project???

  • @umamaheswari1410
    @umamaheswari1410 6 ปีที่แล้ว +1

    Sir what..?? if that searching element is less then the first value...