0% found this document useful (0 votes)
0 views3 pages

CO1030 Lab 03 Search and Recursive Algorithms

The lab aims to teach students the implementation of binary search (iterative and recursive) and recursive pathfinding in Python. Students will work with sorted arrays to find target values and analyze algorithmic behavior, including handling edge cases. The lab includes specific input/output requirements, reflection questions, and a grading rubric, along with a note on acknowledging the use of AI tools in their reports.

Uploaded by

htdprimebot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views3 pages

CO1030 Lab 03 Search and Recursive Algorithms

The lab aims to teach students the implementation of binary search (iterative and recursive) and recursive pathfinding in Python. Students will work with sorted arrays to find target values and analyze algorithmic behavior, including handling edge cases. The lab includes specific input/output requirements, reflection questions, and a grading rubric, along with a note on acknowledging the use of AI tools in their reports.

Uploaded by

htdprimebot
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Department of Computer Engineering

University of Peradeniya
CO1030: Data Structures and Algorithms I
Lab 03: Exploring Binary Search — Iterative and Recursive Approaches

18th of June 2025

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.

Problem: Finding a Value Using Binary Search


You are given an array of integers representing strength values of items. The array is not sorted. You are
also given a target value. Your task is to apply binary search to determine the index of the target value.
Use the provided skeleton code binary search skeleton.py as a starting point for your implementation.

Write a Python program that:


• Takes input from standard input in the specified format.
• Outputs the result in the specified format.
• Includes 2 functions that implement binary search iteratively and recursively

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

From iterative binary search : Target value is present at index 12


From recursive binary search : Target value is present at index 12

Sample Input 2

199
5
180 200 175 150 120

Sample Output 2

From iterative binary search : Target value is not found !


From recursive binary search : Target value is not found !

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.

Reflection and Analysis


1. Describe how your iterative and recursive binary search implementations work, using the Sample
Input 1 above as an example. Use a tool like https://pythontutor.com/ to visualize how your
code executes, and include screenshots of the visualization in your description.

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

Acknowledgment of AI Generative Tools


If you have used any generative AI tool (e.g. ChatGPT, Bard, GitHub Copilot, etc.) to help complete any
part of these laboratory exercises, you must include an acknowledgment in your lab report. Please do the
following:
• Where to place it: Include the acknowledgment at the end of your report.
• Exact wording: Copy and paste (and adapt) this sentence:

I acknowledge the use of [AI tool or technology name] [link] to support specific components
of these laboratory exercises.

eg: I acknowledge the use of ChatGPT [https://chat.openai.com/] 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy