- Iterative code explain:
- 1. lowerBound<=upperBound:
- 2. mid=(lowerBound+upperBound)//2
- 3. DataToFind=int(DataToFind)
- 4. Return
- 5. Print VS Return
- 1. Console Output (printStatement):
- 2. Return Value (returnStatement): - Recursive binary search
- Binary Search: Iterative vs Recursive

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
- A function terminates as soon as it hits a
return. - Code after
returnin the same block is skipped. - Use
returnto both exit and optionally pass a value back.
5. Print VS Return
- The
printstatement is for human-readable feedback in the console. - The
returnstatement provides a value for use in the program's logic. - They are independent of each other.
1. Console Output (print Statement):
- The
printstatement is used to display information on the screen or console. - It is for debugging, logging, or providing feedback to the user.
- It does not affect the function's return value or its logic.
- Example:
print("Hello, World!") # Outputs: Hello, World!`
2. Return Value (return Statement):
- The
returnstatement ends the function execution and specifies the value that the function sends back to its caller. - A
returnstatement determines the value that a function provides as output, but it does not produce console output. - Example:
def add(a, b): return a + b result = add(2, 3) # result now holds the value 5`
Recursive binary search
- base case 1: when value not found
- base case 2: when value found
- general case 1: when value larger than array[mid], need to change first mid +1
- 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 |