Project instructions

In this project, you are to implement the SortedList interface (download the attached SortedList.java file and include that in your project).

A SortedList allows for three operations:

  • insert(): Adds an element to the list
  • get(): Gets the "i"-th smallest number in the list
  • size(): Returns the size of the list.

One should be able to use a SortedList by adding elements in any order, and then iterating through the list and getting the elements back in sorted order.

For example:

SortedList l;
// some code which initializes l
l.insert(5);
l.insert(3);
l.insert(7);
l.insert(20);
l.insert(-30);

for (int i = 0; i < l.size(); i++) {
System.out.println(l.get(i));
}

This should output:

-30
3
5
7
20

Directions

This project has two parts: a programming part and a written part. For the programming part:

  • Implement the SortedList interface.
  • Write a main class which tests out the interface.

For the written part, you must determine the asymptotic running time ("Big Oh") of all of the functions. You should explain the tradeoffs: for example, you may have decided to make the insert method faster at the expense of the get method, or the other way around. Describe which situations would be appropriate for this tradeoff. For example, why would you want get to be fast at the expense of insert? (What kind of application would that be appropriate for?)

SortedList


/**
* An interface which represents a Sorted List of integers.
* Implementations do not necessarily need to keep their lists sorted, but they need to return the elements
* in sorted order.
*/
public interface SortedList {

/**
* Inserts number into the list
*
* @param number
*/
void insert(int number);

/**
* Gets the "i"-th smallest number in the list.
*
* @param i
* @return the i-th smallest number in the list
* @throws IndexOutOfBoundsException if i is not between 0 and size() - 1
*/
int get(int i);

/**
* Returns the number of elements in this list.
*
* @return the number of elements in this list
*/
int size();
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.