Lab 1

Lab 1

Overview

In this lab, you will design an efficient algorithm to search for some target item in a custom data structure called SortedList. If the item exists, you will return the index with the target, otherwise return -1.

Below are the relevant properties of the SortedList you will need for this lab. You can find more details about how the SortedList is implemented in Step 4 of this lab.

  1. A SortedList is a finite list of items of unknown length in which all of the items are sorted in ascending order

  2. This data structure has only one operation: get(index) which runs in O(1) time. This operation returns:
    • the item at the position index, if index is less than the number of items in the list OR
    • null, if index is greater than or equal to the number of items in the list
  3. The data structure does not have an operation that returns the size, or the number of items in, the list.

Your goal is to design an algorithm that can genereate the correct answer while minimizing the number of get queries.

Input:

Output:

Step 0: Creating a markdown file for writeups

Step 1: Comparing two naive algorithms

There are two naive approaches you can take to search for a target item in the SortedList data structure.

  1. Linear search: For each index, starting from 0, check if the item at the index is either the target or null. If the item is target, return the index. If the item is null, return -1.

  2. Find end + Binary search: For each index, starting from 0, check if the item at the index is null. Once you find null, you know the size of the list. Use the size to run the standard binary search algorithm.

Given a SortedList L with n items (although you don’t know what n is), your task is to determine which algorithm to use in two scenarios: when you are looking for just one item, and when you are looking for k different items.

In your write-up include:

Step 2: Designing a more efficient algorithm

Design, describe and analyze an algorithm that is more efficient than the two naive approaches described above. In your write-up include:

Step 3: Submit the write-up

Each member of the team should:

Step 4: Understand the homework requirements

For homework, you will complete a Java implementation of the algorithm for the problem described above. You will want to work towards the details of the implementation in lab, after you’ve completed the writeup. The specification for the implementation is described below.

Starter files

Two starter files are provided:

You should not make any changes to SortedList.java; any code for your implementation should be in AmbiguousEndSearch.java or any helper files / classes.

These are both available from the Course Website (where you found the description for this lab).

Details about the SortedList data structure

The structure supports generic types, so that you can have a list of any data type that supports comparison in Java. In the declarations in the SortedList.java file, and in the descriptions below, the generic type T of items in a SortedList <T> must support comparison, and thus T is declared as follows:

public class SortedList <T extends Comparable<? super T>>

which is the standard way of saying that you can make lists of items of a type T as long as you can compare those items with each other.

Here is a description of some methods you may want to use.

Your implementation

You must implement the following function, declared in the AmbiguousEndSearch.java starter file:

public static <T extends Comparable<? super T>> int locateItem(SortedList<T> list, T target)

The parameter list is a reference to a SortedList data structure of elements of comparable type T, and the parameter target is a reference to the item to locate. You can assume that neither of these references are null.

The function should return –1 if target is not in list; otherwise, it should return the position of the item in the list.

Step 5 (ideally complete in lab): Program Design

Your team should create a plan for how to translate the algorithm for the problem into an implementation that satisfies the requirements above. Before you leave lab, your team should have the TA/instructor review as much of your plan as possible.

This program-design plan should be an outline of your implementation. It should consist of:

Step 6 (homework, next week): Coding, Testing, Program Submission

The Moodle page has a link to submit your code. Please submit your AmbiguousEndSearch.java file only.

You do not need to submit the SortedList.java file.

Scoring

To get scores call “java ComputeScores” after compiling your source code. It will call your AmbiguousEndSearch.locateItem() function with different instances of SortedList and different targets, and will check that your function returns the correct value.

Some of the tests may focus on efficiency and will check the number of get queries your code makes.

Please note: Code that doesn’t compile and run, or code that times out, will receive a zero score.

Remember the score provided by the autograder for a given assignment will make up 90% of your homework score for that assignment. The other 10% will be based on the structure of the code, test cases and comments that explain both the logic of your code and the test cases.