CO1030 Lab 03 Search and Recursive Algorithms
CO1030 Lab 03 Search and Recursive Algorithms
University of Peradeniya
CO1030: Data Structures and Algorithms I
Lab 03: Exploring Binary Search — Iterative and Recursive Approaches
Aim
To practice the implementation of binary search (both iterative and recursive) on sorted arrays and recursive
pathfinding in grid-based maps using Python.
Learning Objectives
Upon completing this lab, you will be able to:
• Implement both iterative and recursive versions of binary search in Python.
• Apply recursion to solve grid traversal problems in Python.
• Handle standard input/output formatting in Python.
• Analyze algorithmic behavior and edge cases.
General Instructions
For each of the following problems, students are required to:
• Implement the algorithms in Python.
• Read inputs from standard input and print outputs to standard output in the specified formats.
• Answer the associated Reflection and Analysis for the problem(s) in a single PDF file.
Input Format
• An integer S: the target value to search for.
• An integer N: the number of items.
• A list of N space-separated integers.
1
Output Format
• If the target is found at index i, output: Target value is present at index i.
• If the target is not found, output: Target value is not found!
Sample Input 1
228
19
283 158 188 166 100 175 273 257 179 296 214 258 228 183 181 224 279 286 185
Sample Output 1
Sample Input 2
199
5
180 200 175 150 120
Sample Output 2
Testing Support: To help you test your implementation, a file named sample inputs.txt is provided
with 50 test cases. Each test case follows the input format described in the problem. You can copy and
paste individual test cases into your program’s standard input. This file is intended to help you verify
the correctness of both your iterative and recursive binary search implementations.
2. What are the advantages and disadvantages of using iteration versus recursion for binary search?
3. For your iterative binary search implementation, analyze how the number of loop iterations
changes with input size. Use the inputs from provided plot inputs.txt file. You are expected
to modify your existing implementation to count how many times the loop runs during the search
process. For each input, record both the input size and the corresponding number of loop iterations
in a suitable data structure. Then, use this data to generate a plot with the input size on the x-axis,
and the number of loop iterations on the y-axis. You may use any plotting tool you prefer.
2
4. For your recursive binary search implementation, analyze how the number of recursive calls
changes with input size. Use the same inputs from plot inputs.txt. Modify your code to count
how many recursive calls are made during the search process. For each input, record the input size
and the corresponding number of recursive calls in a data structure. Then generate a plot with the
input size on the x-axis, and the number of recursive calls on the y-axis. You may use any plotting
tool you prefer.
Hint: For the two plotting tasks above, you may modify your code to read directly from the
plot inputs.txt file. This will allow you to run your binary search implementation(s) on all
50 sample inputs in a single execution of your program, making it easier to collect the data needed
for both plots.
5. What happens if the array contains duplicate values? How does your code behave?
Note: Submit your code including all the changes you did for the plotting task.
Grading Rubric
Criterion Points
Correctness of Python code 40
Input/output typing and formatting accuracy 20
Conceptual understanding 40
I acknowledge the use of [AI tool or technology name] [link] to support specific components
of these laboratory exercises.
• Multiple tools: If more than one tool was used, include the list of tools in the acknowledgment.
Submission
You must submit the following four files:
• exxyyy lab03 problem.py – Python source file for problem
• exxyyy lab03 report.pdf – A single PDF file containing answers to the questions in ”Reflection and
Analysis”.
Upload both files to FEeLS by the given deadline.