Professional Documents
Culture Documents
Chapter outline
sequential and binary search sorting shuffling custom ordering with Comparators
Program efficiency
Sequential search
sequential search: One that examines each element of a list in sequence until it finds the target value or reaches the end of the list.
binary search: An algorithm that searches for a value in a sorted list by repeatedly eliminating half the list from consideration.
Can be written iteratively or recursively implemented in Java as method Arrays.binarySearch in java.util package
Start out with the search area being from indexes 0 to length-1. Examine the element in the middle of the search area.
If it is K, stop. Otherwise,
If it is smaller than K, eliminate the upper half of the search area. If it is larger than K, eliminate the lower half of the search area. Repeat the above examination.
5
Using binarySearch
// binary search on an array: int[] numbers = {-3, 2, 8, 12, 17, 29, 44, 58, 79}; int index = Arrays.binarySearch(numbers, 29); System.out.println("29 is found at index " + index);
Note that the values in the array / list are in sorted order. If they are not, binarySearch is not guaranteed to work properly.
7
Sorting
sorting: Rearranging the values in a list into a given order (often into their natural ascending order).
One of the fundamental problems in computer science Many sorts are comparison-based (must determine order through comparison operations on the input data)
value 15 2 8 1 17 10 12 5 index 0 1 2 3 4 5 6 7
value 1 2 5 8 10 12 15 17
Output:
[c, b, g, h, d, f, e, a] [a, b, c, d, e, f, g, h]
Shuffling
Java has a shuffle method for a List, Collections.shuffle: String[] ranks = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace"}; String[] suits = {"Clubs", "Diamonds", "Hearts", "Spades"}; ArrayList<String> deck = new ArrayList<String>(); for (String rank : ranks) { // build sorted deck for (String suit : suits) { deck.add(rank + " of " + suit); } } Collections.shuffle(deck); System.out.println("Top card = " + deck.get(0));
10
11
Custom ordering
Example: The following code sorts the strings in a casesensitive order, so the uppercase letters come first. We may have wanted a case-insensitive ordering instead.
String[] strings = {"Foxtrot", "alpha", "echo", "golf", "bravo", "hotel", "Charlie", "DELTA"}; Arrays.sort(strings); System.out.println(Arrays.toString(strings));
You can describe a custom sort ordering by creating a class called a comparator.
12
Comparators
The Comparator interface in java.util describes a method for comparing two objects of a given type:
public interface Comparator<T> { public int compare(T o1, T o2); }
The compare method's job is to decide the relative ordering of the two given objects and return an appropriate integer:
<0 0 >0
13
Comparator example
The following Comparator compares Strings, ignoring case: public class CaseInsensitiveComparator implements Comparator<String> { public int compare(String s1, String s2) { return s1.toLowerCase().compareTo( s2.toLowerCase()); } }
14
The sorting methods shown previously can also be called with a Comparator as a second parameter.
The sorting algorithm will use that comparator to order the elements of the array or list.
String[] strings = {"Foxtrot", "alpha", "echo", "golf", "bravo", "hotel", "Charlie", "DELTA"}; Arrays.sort(strings, new CaseInsensitiveComparator()); System.out.println(Arrays.toString(strings)); [Charlie, DELTA, Foxtrot, alpha, bravo, echo, golf, hotel]
15
Program efficiency
reading: 13.2
16
Efficiency
efficiency: A measure of the computing resources used by a program, such as time, memory, or disk space. time efficiency: How quickly a program runs
5 minutes to render a complex 3D scene for a movie is fast. 5 minutes to search Google is slow.
empirical analysis: Program the algorithm, run it, and time it. algorithm analysis: Applying techniques to mathematically estimate the algorithm's runtime without actually coding it.
17
Most individual statements take 1 unit of time to run. When multiple statements are executed sequentially, their runtimes are added.
When statements are executed in a loop, the runtime is multiplied by the number of repetitions of the loop.
18
More examples
Larger statements can also occur sequentially, in which case their runtimes are added. If larger statements are nested, their runtimes are multiplied.
19
most algorithms' runtime can be expressed as a function of the input size N rate of growth: measure of how quickly the graph of a function rises
we only care about very large input sizes (for small sizes, most any algorithm is fast enough) this helps us discover which algorithms will run more quickly or slowly, for large input sizes
20
21
22
Range algorithm 1
Let's examine the efficiency and growth rate of three algorithms for finding the range (difference between largest and smallest element) in an array. First algorithm (looks at all pairs of values to find which pair is the largest and smallest):
// returns the range of numbers in the given array public static int range(int[] numbers) { int maxDiff = 0; for (int i = 0; i < numbers.length; i++) { for (int j = 0; j < numbers.length; j++) { int diff = Math.abs(numbers[j] - numbers[i]); maxDiff = Math.max(maxDiff, diff); } } return maxDiff; }
Copyright 2006 by Pearson Education
23
Runtime of range 1
24
Range algorithm 2
The first algorithm redundantly makes each comparison twice (when i <= j, when i > j).
25
Runtime of range 2
26
Range algorithm 3
We can discover the largest and smallest values in a single pass over the array, rather than using many nested passes over all values. Third algorithm:
public static int range3(int[] numbers) { int max = numbers[0]; int min = max; for (int i = 1; i < numbers.length; i++) { if (numbers[i] > max) { max = numbers[i]; } else if (numbers[i] < min) { min = numbers[i]; } } return max - min; }
Copyright 2006 by Pearson Education
27
Runtime of range 3
Much faster than others Runtime doubles when input size doubles
Copyright 2006 by Pearson Education
28
29
Big-Oh notation
Determined by the exponent of the most frequently executed line of code in the algorithm.
Example: If the most frequently executed line of an algorithm runs approximately N3 times, we say the algorithm is "order N3" or O(N3) for short.
We are concerned with how the function grows when N is large. We are not picky about constant factors.
30
Hierarchy of Big-Oh
constant time logarithmic linear loglinear quadratic cubic ... exponential ...
An algorithm from a lower complexity class will run much faster than one from a higher complexity class when the value of N becomes very large.
31
// O(N)
Adding to the loop counter means that the loop runtime grows linearly when compared to its maximum value N.
Multiplying the loop counter means that the maximum value N must grow exponentially to linearly increase the loop runtime; therefore, it is logarithmic.
Loops in sequence add together their runtimes, which means the loop set with the larger runtime dominates.
for (int i = 0; i < N; i += c) { <statements>; } for (int i = 0; i < N; i += c) { for (int j = 0; j < N; i *= c) { <statements>; } }
Copyright 2006 by Pearson Education
// O(N)
// O(N log N)
33
Approximate the value of the variable sum after the following code fragment, as an expression in terms of input size n. Use Big-Oh notation to describe the algorithm's overall runtime.
int sum = 0; for (int i = 1; i <= N; i *= 2) { sum++; } for (int i = 1; i <= 1000; i++) { sum++; sum++; }
34
Approximate the value of the variable sum after the following code fragment, as an expression in terms of input size n. Use Big-Oh notation to describe the algorithm's overall runtime.
int sum = 0; for (int i = 1; i <= N; i++) { for (int j = 1; j <= i / 2; j += 2) { sum++; } }
35
36
binary search: An algorithm that searches for a value in a sorted list by repeatedly eliminating half the list from consideration.
Start out with the search area being from indexes 0 to length-1. Examine the element in the middle of the search area.
If it is K, stop. Otherwise,
If it is smaller than K, eliminate the upper half of the search area. If it is larger than K, eliminate the lower half of the search area. Repeat the above examination.
37
min
4 5
6
max
38
min
4 5
6
39
4 5
6
40
41
while (min <= max) { int mid = (max + min) / 2; if (numbers[mid] == target) { return mid; // found it! } else if (numbers[mid] < target) { min = mid + 1; // too small } else { // numbers[mid] > target max = mid - 1; // too large } } return -1; }
Copyright 2006 by Pearson Education
// not found
42
How do we analyze the runtime of binary search? The algorithm's runtime is dominated by a while loop. Each pass of that loop cuts the search space in half.
43
44
Selection sort
selection sort: orders a list of values by repetitively putting a particular value into its final position more specifically:
find the smallest value in the list switch it with the value in the first position find the next smallest value in the list switch it with the value in the second position repeat until all values are in their proper places
45
46
63
27
72
64
58
14
1
1
9
9
27
14
72
72
64
64
58
58
14
27
63
63
47
48
makes sense because the code performs two nested loops over the array of size N its runtime should be O(N2)
49
Perform the selection sort algorithm on this data. Write the contents of the array after each pass of the outermost loop of selection sort until the array is sorted.
50
51
Merge sort
merge sort: orders a list of values by recursively dividing the list in half until each sub-list has one element, then recombining
Invented by John von Neumann in 1945 relies on observation that if you have two sorted lists of values, they can be combined into a single larger sorted list quickly
split the array into two halves. merge sort the left half (recursively). merge sort the right half (recursively). combine the two halves into a sorted whole.
52
98 23
98 23
45 14
45 14
6
6
67
67
33 42
33 42
23 98
14 45
6
6
67
33 42
14 23 45 98 6
33 42 67
14 23 33 42 45 67 98
53
21
18
20
7
13
0
21
18
3
9
4
20
7
13
0
6
1
21
2
18
3
9
4
4
5
8
6
20
7
13
0
6
1
21
2
18
3
9
4
4
5
8
6
20
7
6
0
13
1
18
2
21
3
4
4
9
5
8
6
20
7
6
0
13
18
21
3
4
4
20
7
4
0
13
18
20
21
7
54
merge operation:
Given two sorted arrays, merge operation produces a sorted array with all the elements of the two arrays
A 6 13 18 21 B 4 8 9 20
13
18
20
21
Running time of merge : O(N), where N is the number of elements in the merged array.
When merging two sorted parts of the same array, we'll need a temporary array to store the merged whole.
55
56
57
Merge code
58
// merge the sorted halves into a sorted whole merge(array, left, right);
} }
59
Perform the merge sort algorithm on this data. Write the contents of the array after each of the recursive calls of merge sort have finished.
60