Python Program For Binary Search

Sreemeenakshi V
2 min readJun 5, 2021

Language Used: Python

Binary Search

Binary Search is basically used to find an item from a sorted list of items. It works by dividing the list into half repeatedly until it is narrowed down to just one.

It is time efficient, that is it’s run time is faster than that of the Linear search. If the value of the search key is greater than the middle element, it narrows the interval to the upper half, otherwise to the lower half.

Concepts Involved

While Loop:

While loops in Python are used to execute a block of statements repeatedly until a given condition is satisfied. It falls under the category of Indefinite Iteration. It is generally used when we don’t know the number of times to iterate beforehand.

Syntax of While loop:

while test_expression:
Body of while

Here, the test_expression is checked first and only when it is True, it enters the loop. After every single iteration, the test expression is checked again. This process is continued until the test_expression becomes false.

Flowchart of While loop:

Syntax of if_else statements:

if test expression:
Body of if
else:
Body of else

Here, the body of if statement is executed only when the if condition is True, and if the condition is False, the body of else is executed.

Flowchart of if_else:

The print statement is used to display a particular statement, whose syntax is print()

Script

Output

False
True

To excel in programming and for other Educational assistance,

Refer: https://www.guvi.in/

--

--