Question

  • Creator
    Topic
  • #4061347

    Python Binary Search Algorithm Error

    by gulshan212 ·

    Hello this is Gulshan Negi
    Well, I am creating a program for binary search algorithm using Python program but there is a problem in its execution, I don’t know what I am doing wrong here.
    Source Code:-

    def binary_search(a_list, an_item):
    first = 0
    last = len(a_list) – 1

    while first <= last:
    mid_point = (first + last) // 2
    if a_list[mid_point] == an_item:
    return True
    else:
    if an_item < a_list[mid_point]:
    last = mid_point – 1
    else:
    first = mid_point + 1
    return False

    def binary_search_rec(a_list, first, last, an_item):
    if len(a_list) == 0:
    return False
    else:
    mid_point = (first + last) // 2
    if a_list[mid_point] == an_item:
    return True
    else:
    if an_item < a_list[mid_point]:
    last = mid_point – 1
    return binary_search_rec(a_list, first, last, an_item)
    else:
    first = mid_point + 1
    return binary_search_rec(a_list, first, last, an_item)

    if __name__ == ‘__main__’:
    a_list = [1, 4, 7, 10, 14, 19, 102, 2575, 10000]

    print(‘Binary Search:’, binary_search(a_list, 4))
    print(‘Binary Search Recursive:’,
    binary_search_rec(a_list, 0, len(a_list) 1, 4))

    Can anyone give their suggestions on this, would be a great support in this regard.
    Thanks

    Note: promotional URL removed by moderator.

    • This topic was modified 1 year, 3 months ago by Avatar photokees_b.

You are posting a reply to: Python Binary Search Algorithm Error

The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our Community FAQs for details. All submitted content is subject to our Terms of Use.

All Answers

  • Author
    Replies
    • #4061379
      Avatar photo

      Re: algorithm

      by kees_b ·

      In reply to Python Binary Search Algorithm Error

      You don’t tell what problem you get, and that makes it difficult to offer a solution.

      if you learn programming, as you seem to do, you must also learn to debug. Either runtime, by setting breakpoints and looking at the variables, or by printing intermediate results after each step.

      I’m sure you’ll manage to solve this.

    • #4061422
      Avatar photo

      Hmm, ChatGPT seems to find bugs.

      by rproffitt ·

      In reply to Python Binary Search Algorithm Error

      “There are a few issues with the code:

      The subtraction symbol in lines 2 and 20 appears to be an en-dash instead of a hyphen, which can cause a syntax error. Replace these with regular hyphens.

      In the recursive binary search function, the base case should check if first > last instead of checking if the list is empty. Change the if statement in line 12 to if first > last:

      Here’s the corrected code:
      def binary_search(a_list, an_item):
      first = 0
      last = len(a_list) – 1

      while first <= last:
      mid_point = (first + last) // 2
      if a_list[mid_point] == an_item:
      return True
      else:
      if an_item < a_list[mid_point]:
      last = mid_point – 1
      else:
      first = mid_point + 1
      return False

      def binary_search_rec(a_list, first, last, an_item):
      if first > last:
      return False
      else:
      mid_point = (first + last) // 2
      if a_list[mid_point] == an_item:
      return True
      else:
      if an_item < a_list[mid_point]:
      last = mid_point – 1
      else:
      first = mid_point + 1
      return binary_search_rec(a_list, first, last, an_item)

      if __name__ == ‘__main__’:
      a_list = [1, 4, 7, 10, 14, 19, 102, 2575, 10000]

      print(‘Binary Search:’, binary_search(a_list, 4))
      print(‘Binary Search Recursive:’, binary_search_rec(a_list, 0, len(a_list) – 1, 4))

      Note that in the main function, the second call to binary_search_rec has a typo where len(a_list) 1 should be len(a_list) – 1. This has been fixed in the corrected code above.”

Viewing 1 reply thread