Chapter 14 – Sorting and Searching

Sections 14.6 - 14.8

Searching

section_6_1/LinearSearcher.java

Your browser does not support the <object> tag.

section_6_1/LinearSearchDemo.java

Your browser does not support the <object> tag. Program Run:

Lecture 12 Clicker Question 1

Suppose you have 1,000,000 address records, with name, street address, email address, and telephone number. And suppose you need to find someone with a specific telephone number. How many records do you expect to search before finding the number?

  1. Just one—the one with the desired number
  2. About 500,000
  3. 1,000,000
  4. 1,000,000 squared

Binary Search

Binary Search

section_6_2/BinarySearcher.java

Your browser does not support the <object> tag.

Binary Search

Binary Search

Binary Search

Lecture 12 Clicker Question 2

Suppose you need to look through a sorted array with 1,000,000 address records that are already sorted by telephone number. Using the binary search algorithm, how many records do you expect to search before finding a given number, or conclude that it is not present?

  1. About 20
  2. About 500,000
  3. 1,000,000
  4. 1,000,000 squared

Problem Solving: Estimating the Running Time of an Algorithm - Linear time

Problem Solving: Estimating the Running Time of an Algorithm - Linear time

Problem Solving: Estimating the Running Time of an Algorithm - Quadratic time

Problem Solving: Estimating the Running Time of an Algorithm - Quadratic time

The Triangle Pattern

Problem Solving: Estimating the Running Time of an Algorithm - Logarithmic time

Problem Solving: Estimating the Running Time of an Algorithm - Logarithmic time

Lecture 12 Clicker Question 3

What is the big-Oh running time of the following algorithm to check whether the first element is duplicated in an array?

for (int i = 1; i < a.length; i++)
{
   if (a[0] == a[i]) { return true; }
}
return false;
  1. O(log n)
  2. O(n)
  3. O(n log n)
  4. O(n2)

Lecture 12 Clicker Question 4

What is the big-Oh running time of the following algorithm to check whether an array has a duplicate value?

for (int i = 0; i < a.length; i++)
{
   for (j = i + 1; j < a.length; j++)
   {
      if (a[i] == a[j]) { return true; }
   }
}
return false;
  1. O(log n)
  2. O(n)
  3. O(n log n)
  4. O(n2)

Sorting and Searching in the Java Library - Sorting

Sorting and Searching in the Java Library - Binary Search

Comparing Objects

Comparing Objects

Lecture 12 Clicker Question 5

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