Sections 14.6 - 14.8
[46, 99, 45, 57, 64, 95, 81, 69, 11, 97, 6, 85, 61, 88, 29, 65, 83, 88, 45, 88] Enter number to search for, -1 to quit: 12 Found in position -1 Enter number to search for, -1 to quit: -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?


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?
int count = 0;
for (int i = 0; i < a.length; i++)
{
if (a[i] == value) { count++; }
}

boolean found = false;
for (int i = 0; !found && i < a.length; i++)
{
if (a[i] == value) { found = true; }
}


for (int i = 0; i < a.length; i++)
{
counts[i] = Count how often a[i] occurs in a
}


int count = 0;
for (int i = 0; i < a.length; i++)
{
count++;
if (i == a.length - 1 || a[i] != a[i + 1])
{
counts[i] = count;
count = 0;
}
}

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;
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;
Arrays.sortArrays.binarySearchCollections.sortComparable objects, or supply Comparatorint[] a = { 1, 4, 9 };
int v = 7;
int pos = Arrays.binarySearch(a, v);
// Returns –3; v should be inserted before position 2
public interface Comparable
{
int compareTo(Object otherObject);
}
public class Country implements Comparable
{
public int compareTo(Object otherObject)
{
Country other = (Country) otherObject;
if (area < other.area) { return -1; }
else if (area == other.area) { return 0; }
else { return 1; }
}
}
Country[] countries = new Country[n]; // Add countries Arrays.sort(countries); // Sorts by increasing area
You are given an ArrayList<Rectangle> and want to sort it by increasing area. What do you do?
Arrays.sort and take advantage of the fact that Rectangle implements ComparableArrays.sort and provide a suitable Comparator objectCollections.sort and take advantage of the fact that Rectangle implements ComparableCollections.sort and provide a suitable Comparator object