CS 240: Algorithms and Data Structures
James Madison University, Spring 2023

Profiling Search Algorithms

Introduction

Your goal in this lab is to learn about the linear and binary search algorithms and profile these algorithms to measure their cost. You will do this by writing a utility that counts the number of comparisons made by both search algorithms and produces a report of their best, worst, and average cases.

SearchResult

In the context of this activity, a search result is a tuple of three values: an item being searched for, a boolean indicating whether the item was found, and a number of comparisons that the search needed to determine its answer. Your first task is to make a class that encapsulates this 3-tuple. It has no setters, only getters. Such a class can be cranked out in moments using an IDE like IntelliJ or Eclipse:

That’s all there is to this class. Most of the work will be done in SearchProfiler.

SearchProfiler

The SearchProfiler class must have a type parameter T. Again, use the extends keyword to ensure that the type argument will implement the Comparable interface.

SearchProfiler must have the following public interface. As you implement this class test each method before moving on the the next one.

Your instructors are not providing the source code for JUnit tests in this lab. When your instructors provide tests, students often do not test code themselves and enter a frustrating game of trying to satisfy the grading machine. Please don’t do that. Test your code locally. Reason about its correctness. You should only submit to Gradescope after you are reasonably confident that your implementation is correct.

Experimentation

Once you are confident that your implementation is correct, add the following method to your SearchProfiler class or to a separate driver class:

BEFORE you run anything, take a minute to think about what you expect to see when this method is called as follows:

Write your predictions in a file named experiments.txt, then run the test. Update the experiments.txt with the actual output as well as a sentence or two explaining the results.

Repeat this process for the following call:

Predict the results, find the actual results, and explain any discrepancies.

Submission

Submit SearchResult.java, SearchProfiler.java and experiments.txt through GradeScope.

Acknowledgments

The original version of this lab was developed by Chris Johnson.