📖 Scenario: You have a sorted list of book page numbers. You want to find if a certain page number exists in the book quickly.
🎯 Goal: Build a program that uses the binary search iterative method to find a page number in a sorted list.
📋 What You'll Learn
Create a sorted list of page numbers
Set the target page number to search for
Implement the binary search using a while loop
Print the index of the target page if found, or -1 if not found
💡 Why This Matters
🌍 Real World
Binary search is used in many applications like searching in phone books, dictionaries, or any sorted data quickly.
💼 Career
Understanding binary search helps in software development roles where efficient searching algorithms improve performance.
Progress0 / 4 steps
1
Create the sorted list of page numbers
Create a list called pages with these exact values: [3, 8, 15, 23, 42, 56, 78, 91]
DSA Python
Hint
Use square brackets and commas to create the list exactly as shown.
2
Set the target page number to search
Create a variable called target and set it to 42
DSA Python
Hint
Just assign the number 42 to the variable named target.
3
Implement the binary search iterative logic
Write code to perform binary search iteratively using variables left, right, and mid. Use a while loop that runs while left <= right. Inside the loop, calculate mid as the middle index, compare pages[mid] with target, and adjust left or right accordingly. Create a variable result that stores the index if found, or -1 if not found.
DSA Python
Hint
Use a while loop and update left and right pointers based on comparison with target.
4
Print the search result
Print the value of the variable result to show the index of the target page or -1 if not found.
DSA Python
Hint
Use print(result) to show the index.
Practice
(1/5)
1. What is the main purpose of the mid variable in the iterative binary search algorithm?
easy
A. To store the target value we are searching for
B. To keep track of the number of iterations
C. To find the middle index of the current search range
D. To hold the entire list being searched
Solution
Step 1: Understand the role of mid in binary search
The mid variable calculates the middle index between the current low and high indices to split the search range.
Step 2: Explain why mid is important
By checking the element at mid, the algorithm decides whether to search the left or right half next.
Final Answer:
To find the middle index of the current search range -> Option C
Quick Check:
mid = middle index [OK]
Hint: Remember binary search splits list by middle index [OK]
Common Mistakes:
Confusing mid with the target value
Using mid as a counter instead of index
Not updating mid inside the loop
2. Which of the following is the correct way to calculate the middle index mid in an iterative binary search in Python?
easy
A. mid = (low + high) // 2
B. mid = low - high // 2
C. mid = low + high
D. mid = (low + high) / 2
Solution
Step 1: Identify correct integer division in Python
In Python, // performs integer division, which is needed for an index.
Step 2: Explain why / is incorrect
/ returns a float, which cannot be used as a list index.
Final Answer:
mid = (low + high) // 2 -> Option A
Quick Check:
Use integer division for index [OK]
Hint: Use // for integer division in Python [OK]
Common Mistakes:
Using / which returns float
Adding low and high without division
Incorrect operator precedence without parentheses
3. What will be the output of the following code snippet?