CS 46B - Lecture 12

Cover page image

Pre-class reading

Linear Search

Binary Search

Lecture 12 Clicker Question 1

Binary search seems so much better than linear search. Should one sort the array first to take advantage of it?

  1. Yes, always
  2. Only if one does at least n searches
  3. Only if one does at least n2 searches
  4. No, it's never a good idea. Linear search isn't that much slower.

Sorting Real Data

Lecture 12 Clicker Question 2

You are given an ArrayList<Rectangle> and want to sort it by increasing area. What do you do?

  1. Call Arrays.sort and take advantage of the fact that Rectangle implements Comparable
  2. Call Arrays.sort and provide a suitable Comparator object
  3. Call Collections.sort and take advantage of the fact that Rectangle implements Comparable
  4. Call Collections.sort and provide a suitable Comparator object

Summary