-
Hajipur, Bihar, 844101
Hajipur, Bihar, 844101
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms
Binary Search is an efficient algorithm to find a value in a sorted list by repeatedly dividing the search interval in half.
It’s faster than linear search but only works on sorted data.
Find the middle of the list
If the middle value equals the target → return index
If the target is smaller → search the left half
If the target is larger → search the right half
Repeat until found or range becomes empty
def binary_search(arr, target):
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1
numbers = [10, 20, 30, 40, 50, 60, 70]
target = 40
result = binary_search(numbers, target)
print("Found at index:", result)
✅ Output:
Found at index: 3
def binary_search_recursive(arr, low, high, target):
if low > high:
return -1
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
return binary_search_recursive(arr, low, mid - 1, target)
else:
return binary_search_recursive(arr, mid + 1, high, target)
Case | Time |
---|---|
Best Case | O(1) |
Worst Case | O(log n) |
Average | O(log n) |
Searching in sorted arrays/lists
Dictionary search (like Python’s bisect
module)
Logarithmic time search
Finding boundaries in numerical ranges
Q1. Write a Python program to perform binary search on a sorted list of 10 elements.
Q2. Write a Python program to modify the binary search function to return True
or False
instead of index.
Q3. Write a Python program to accept a list and a target value from the user and search using binary search.
Q4. Write a Python program to implement binary search using recursion.
Q5. Write a Python program to count and print the number of comparisons made during binary search.
Q6. Write a Python program to return all occurrences of the target value in case of duplicates.
Q7. Write a Python program to perform binary search on a descending sorted array (reverse order logic).
Q8. Write a Python program to use the bisect
module to find insertion point of a value in a sorted list.
Q9. Write a Python program to compare execution time of binary search vs linear search.
Q10. Write a Python program to handle invalid input types using try/except in binary search.
Introduction to Python
Python Basics
Python Syntax
Python Comments
Python Variables
Python Data Types
Python Casting
Python I/O
Python Operators
Cotrol Structures
Data Structures
Python Strings
Python Lists
Python Tuples
Python Dictionaries
Python Sets
Python Arrays
Python Bytes and Bytearray
Date and Time
Functions and Module
File Handling
Python OOP
Advanced Concepts
Python Scope
Python Modules
Python JSON
Python RegEx
Python PIP
Python Try...Except
Python String Formatting
Python User Input
Python VirtualEnv
Python Math
Python DSA
Python DSA
Lists and Arrays
Python Stacks
Python Queues
Linked Lists
Python Hash Tables
Python Trees
Python Binary Trees
Binary Search Trees
Python AVL Trees
Python Graphs
Searching Algorithms
Sorting Algorithms