100% found this document useful (1 vote)
81 views

Randomized Binary Search Algorithm

The document describes a recursive binary search algorithm. 1. It generates a random list of numbers between 0-100 with a step of 2. It then asks the user to input a number and checks if it is present in the list. 2. The algorithm divides the list in half at each step and checks if the number matches the middle element. If not present, it recursively searches in the lower or upper half based on the number's value. 3. The code defines functions to generate the random list, perform the divide and conquer search recursively, and take user input to check for the number in the list.

Uploaded by

medha tripathi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
81 views

Randomized Binary Search Algorithm

The document describes a recursive binary search algorithm. 1. It generates a random list of numbers between 0-100 with a step of 2. It then asks the user to input a number and checks if it is present in the list. 2. The algorithm divides the list in half at each step and checks if the number matches the middle element. If not present, it recursively searches in the lower or upper half based on the number's value. 3. The code defines functions to generate the random list, perform the divide and conquer search recursively, and take user input to check for the number in the list.

Uploaded by

medha tripathi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Recursive Binary search algorithm

By Medha Tripathi
601962011
Problem statement:
• Create a random list of numbers between 0 and 100 with a
difference of 2 between each number.
• Ask the user for a number between 0 and 100 to check whether
their number is in the list.
• The programmer should work like this. The program will half the
list of numbers and see whether the users number matches the
middle element in the list. If they do not match, the programmer
will check which half the number lies in, and eliminate the other
half. The search then continues on the remaining half, again
checking whether the middle element in that half is equal to the
user’s number. This process keeps on going until the programmer
finds the users number, or until the size of the sub array is 0,
which means the users number isn't in the list.
Recursive Binary search Algorithm:
• This search algorithm works on the principle of
"Divide and Conquer".
• Recursive algorithm, a function calls itself again
and again till the base condition(stopping
condition) is satisfied
• In the case of Iterative algorithms, a certain set
of statements are repeated a certain number of
times until it is done.
Code :
import random Explanation:
def rlist(): 1.Importing random lib from system.
2.Defining a function named rlist()
a = [] 3.Initiating the list with null array
for i in range(0,10): 4.Now we iterate the sequence for 10
num = random numbers.
random.randrange(0,101,2 5. Now we implements pseudo-random
) number generators for distributions for
producing selections over an arbitrarily
a.append(num) large range here it is 0 to 101 with step
print(*a) size 2 and append the input number into
return a list as an object since random is also a
kind of class.
Continued:
def divide(a,c): 1.Define a function named divide
l=len(a) for recursive coding with two
parameters i.e. list name and
mid = int(l/2) input item.
if(a[mid]==c): 2.Now we define midpoint and
print("Your input is check the input at midpoint.
If item is present we stop there
present") but if not we proceed for other
else: cases.
Continued: else cases
else: Now other cases:
flag = 0 1.The continue statement
for i in range(mid): returns the control to the
if a[i]==c: beginning of the loop.
flag = 1 2.Flag checks the condition
is true or false. If condition is
continue
not satisfied then it moves
if flag == 0: forward and continue.
for j in range((mid+1),l): 3.Here the program checks
if a[j]==c: the condition for second half.
flag = 1 4.If present flag becomes
continue one and if not it shows your
if flag == 0: input is not present.
print("Sorry, your input is not
present")
Continued:
if flag == 1: If the no belongs to the first half
t = [] , then export the first half to a
for j in range(mid): temporary list called t[]
t.append(a[j]) Now call the splitter function
with t[] as the input, remember
divide(t,c)
t[] contains only the half of the
original list
elif flag == 0: This recursive calling will stop,
t = [] until the "Number is present".
for j in range((mid+1),l): If the no belongs to the second
half, then export the second
t.append(a[j])
half to a temporary list called
divide(t,c) t[]
Continued:

a = rlist()
c = int(input("Please enter the no: "))
divide(a,c)

•Calling list and divide function and asking user to input their
respective number
Outputs:

If number is present If number is not in list


Thank you

You might also like