0% found this document useful (0 votes)
8 views

LAB1 - Python

The document provides an overview of recursive and iterative binary search algorithms. It includes pseudocode for both approaches and outlines objectives to write Python functions to implement the algorithms and test them on sample input data.

Uploaded by

abdulmjeedbasllo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

LAB1 - Python

The document provides an overview of recursive and iterative binary search algorithms. It includes pseudocode for both approaches and outlines objectives to write Python functions to implement the algorithms and test them on sample input data.

Uploaded by

abdulmjeedbasllo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab Manual (Design and Analysis of Algorithm)

Experiment # 1
Lab Instructor: Eng Kamal Al-homidi
Recursive and Iterative Binary Search

Objectives:
1. Determine the time complexity of the Recursive and Iterative Binary Search algorithms
2. Write a two Functions for Recursive and Iterative Binary search in python using the
next pseudo code

pseudo code for Algorithm


#a: Recursive Binary Search
RecursiveBinSearch (n, A, k, Low, High)
// n – number of elements in the array
// A – array[low:high] in increasing order
// k – the key to be searched
//L&H first & last elements
mid ← (Low + High)/2
if Low > High Then return -1
if k = A[mid] Then return(mid)
else if k < A[mid]
Then return RecursiveBinSearch(n, A, k, Low, mid-1)
else
Then return RecursiveBinSearch(n, A, k, mid+1, High)

#b: Iterative Binary Search


IterativeBinSearch(A, n, key)
// A – Given Array – A[0:n-1] in increasing order
// n – number of elements in the array
// k – the key to be searched
low ← 0 high ← n-1
while low ≤ high {
mid = (low + high)/2
if key < A[mid]
Then high = mid -1
else if key > A[mid]
Then low = mid + 1
else
Then return mid
}
Return -1
3. Write a full Program for Iterative Binary Search in python

def iterativeBinarySearch(arr, n, k):


low = 0
high = n - 1
while low <= high:
mid = (low + high) // 2
if k < arr[mid]:
high = mid - 1
elif k > arr[mid]:
low = mid + 1
else:
return mid
return -1

n = int(input("Enter The Size Of Array: "))


arr = []
for i in range(n):
element = int(input(f"Enter Element In array [{i}]: "))
arr.append(element)

k = int(input("Enter Number To Search in Array: "))


index = iterativeBinarySearch(arr, n, k)

if index != -1:
print("Element found at index", index)
else:
print("Element not found in the array")

OutPut:
4. Write a full Program for Recursive Binary Search using the next pseudo code
in python

Pseudo Code Recursive Binary Search


RecursiveBinSearch (n, A, k, Low, High)
// n – number of elements in the array
// A – array[low:high] in increasing order
// k – the key to be searched
//L&H first & last elements
mid ← (Low + High)/2
if Low > High
Then return -1
if k = A[mid]
Then return(mid)
else if k < A[mid]
Then return RecursiveBinSearch(n, A, k, Low, mid-1)
else
Then return RecursiveBinSearch(n, A, k, mid+1, High)

You might also like