1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Binarysearch with Python with recursion

Discussão em 'Python' iniciado por Stack, Março 8, 2025.

  1. Stack

    Stack Membro Participativo

    def binarysearch(a,n):
    mid=int(len(a)/2) #taking the middle point
    if(n==a[mid]):
    return mid
    elif(n>a[mid]): #if greater then do binarysearch in smaller part
    binarysearch(a[(mid+1):],n)
    elif(n<a[mid]):
    binarysearch(a[0:mid],n)


    Hello this is my code and my test case is binarysearch([0,2,4,5],2) and it is returning none but I think it should return 1.Because it goes to elif(n<a[mid]) part in that case and return mid if you gave me minus because of not explaining correct it now `

    Continue reading...

Compartilhe esta Página