1-3067163255.gif|400

def BinarySearch(DataToFind):
	DataToFind=int(DataToFind)
	lowerBound=0
	upperBound=len(DataStored)-1
	#to correctly accessed array index as Py is zero-indexed

  while lowerBound <= upperBound:
    mid = (lowerBound + upperBound)// 2 # only want integer
    if DataToFind == DataStored[mid]:
      print(f'the index to find {DataToFind} is {mid}')
      return 1
      
    elif DataToFind > DataStored[mid]:#back area
      lowerBound = mid+1
      
    elif DataToFind < DataStored[mid]:
      upperBound = mid-1

  return -1

Iterative code explain:

1. lowerBound<=upperBound:

lowerBound can be smaller/ equal to upperBound

2. mid=(lowerBound+upperBound)//2

mod 2

3. DataToFind=int(DataToFind)

function see inputed number as string unless explicitly declared as int()
the error output will be like

elif DataToFind>DataStored[mid]:#upper area
         ^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: '>' not supported between instances of 'str' and 'int'"

4. Return
5. Print VS Return
1. Console Output (print Statement):
print("Hello, World!")  # Outputs: Hello, World!`
2. Return Value (return Statement):
def add(a, b):     return a + b  result = add(2, 3)  # result now holds the value 5`

  1. base case 1: when value not found
  2. base case 2: when value found
  3. general case 1: when value larger than array[mid], need to change first mid +1
  4. general case 2: when value smaller than array[mid], need to change last to mid -1
def binarySearch(array, first = None, last = None, tofind):
    if first is None:
        first = 0
    # set first to zero when uninitialised    
    if last is None:
        last = len(array) -1
    # set last to len when uninitialised
        
    #statement to prevent infinite loop
    #resulted from value not found
    if first> large:
        print)(f'{tofind} is not found.')
        return -1
        
    mid = (first + last)// 2
    #base case
    if tofind == array[mid]:
        print(f'{tofind} is found at index {mid}')
        return mid

    #general case
    elif tofind > array[mid]: # go to back
        return binarySearch(array, mid+1, last, tofind)
    
    else:# go to front 
        return binarySearch(array, first, mid-1, tofind)

Binary Search: Iterative vs Recursive

Aspect Iterative Binary Search Recursive Binary Search
Control Structure Uses a while loop Uses recursive function calls
Loop/Exit Condition while first <= last if first > last: return -1 (base case when not found)
Parameter Handling Updates first and last within the loop Passes updated first and last as function parameters
Code Simplicity Slightly longer but easy to follow More concise and expressive for divide-and-conquer
Memory Usage Constant space (no recursion) Uses call stack (space grows with recursion depth)
Performance (Speed) Very efficient Efficient but has call overhead
Initial State Handling No special handling needed Often uses None to mark first call and store initial values
Stack Use Doesn’t use the function call stack Stack stores intermediate return states (winding/unwinding)
Best Use Case Preferred for large data sets Preferred for small inputs or educational clarity
Code Example Base Condition Inside the while loop Checked before the recursive call