Return to lecture notes index

15-100 Lecture 28 (Friday, November 11, 2005)

Quick Quiz

Please show, step-by-step, a binary seach, looking for 6, within the following list. You should show the selected pivot and the relevant partition, for each iteration.

1   4   5   7   8   9   12   14   16   21   25

                    X

1   4   5   7   8   
        X

            7   8
            X
           ^
           Empty. Not found.
          
            

Today's Challenge

Our goal today is to explore another type of operation on a container, ordered, or otherwise. We want to filter the container, returning a new container of only those items satisfying some criteria. In the process we want to leave the original container unchanged.

This type of method can be implemented on ether an ordered or unordered container. On your final exam, you'll be doing filtering upon an unordered container. Since we've most recently be developing an OrderedContainer, we'll work with that class today.

lessThan(Comparable item)

Our goal is to return a new OrderedContainer that contains all items from this container that are strictly less than the item passed in. So, we know that our signature must be as follows:

public OrderedContainer lessThan (Comparable item)

We know that we need to return a container -- so we must create one. Now, we're here:

public OrderedContainer lessThan (Comparable item) {
  OrderedContainer newContainer = new OrderedContainer();

  ...

  return newContainer;
}

Now, we need to traverse the original OrderedContainer, adding everything that satisifies the criteria to the newContainer:

public OrderedContainer lessThan (Comparable item) {
  OrderedContainer newContainer = new OrderedContainer();

  for (int index=0; index < nextSlot; index++) {
    if (list[index].compareTo(item) < 0)
      newContainer.add (list[index]);
  }

  return newContainer;
}

The code we have above is a complete, be it naive, solution.

Making lessThan(...) Better

The solution we implement above is correct, be it naive. And, given an unsorted list, it would be the best that we can do. But, in an ordered list, it isn't necessary to search the entire array. Instead, we can stop once we get to the first item that is too large -- nothing smaller can follow:

public OrderedContainer lessThan (Comparable item) {
  OrderedContainer newContainer = new OrderedContainer();

  for (int index=0; index < nextSlot; index++) {
    if (list[index].compareTo(item) < 0)
      newContainer.add (list[index]);
    else
      break;
  }

  return newContainer;
}

The getWithinRange Method

Now, let's take the logic above and make it work to solve a more general problem, getting all of those items between lowerBound and upperBound, inclusive:

public OrderedContainer getWithinRange(Comparable lowerBound, 
                                       Comparable upperBound) {
  OrderedContainer newContainer = new OrderedContainer();



  for (int index=0; index < nextSlot; index++) {

    // Skip lesser items
    if (list[index].compareTo(lowerBound) < 0)
      continue;
    
    // End once items become greater
    if (list[index].compareTo(upperBound) > 0)
      break;

    // Add the rest
    newContainer.add (list[index]);
  }

  return newContainer;
}

The greaterThan(...) Method

Now, let's consider the greaterThan(...) method, beginning with a naive rewrite of the lowerThan(...) method described above:

public OrderedContainer greaterThan (Comparable item) {
  OrderedContainer newContainer = new OrderedContainer();

  for (int index=0; index < nextSlot; index++) {
    if (list[index].compareTo(item) > 0)
      newContainer.add (list[index]);
  }
}

The solution above is correct, but not particularly efficient. It skips past the entire beginning just to get to the end. So, why not work from the end toward the beginning? Here's an improved version:

public OrderedContainer greaterThan (Comparable item) {
  OrderedContainer newContainer = new OrderedContainer();

  for (int index=(nextSlot-1); index >= 0; index--) {
    if (list[index].compareTo(item) > 0)
      newContainer.add (list[index]);
  }
}

Next Class

...we'll construct another example, the Set class.