Académique Documents
Professionnel Documents
Culture Documents
Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright 20022010 6/26/10 8:09 AM
Sequential Search
2
Search Client: Exception Filter
3
Searching Challenge 1
A. Toaster
B. Cellphone
C. Your laptop
D. Supercomputer
E. Google server farm
4
Binary Search
5
Twenty Questions
6
Binary Search
Main idea.
Sort the array (stay tuned).
Binary search.
Examine the middle key.
If it matches, return its index.
Otherwise, search either the left or right half.
7
Binary Search: Java Implementation
public static int search(String key, String[] a, int lo, int hi) {
if (hi <= lo) return -1;
int mid = lo + (hi - lo) / 2;
int cmp = a[mid].compareTo(key);
if (cmp > 0) return search(key, a, lo, mid);
else if (cmp < 0) return search(key, a, mid+1, hi);
else return mid;
}
8
Binary Search: Mathematical Analysis
N N/2N/4 N/8 1
Q. How many times can you divide a number by 2 until you reach 1?
A. log2 N.
1
2 1
4 2 1
8 4 2 1
16 8 4 2 1
32 16 8 4 2 1
64 32 16 8 4 2 1
128 64 32 16 8 4 2 1
256 128 64 32 16 8 4 2 1
512 256 128 64 32 16 8 4 2 1
1024 512 256 128 64 32 16 8 4 2 1
9
Searching Challenge 2
A. Toaster
B. Cell phone
C. Your laptop
D. Supercomputer
E. Google server farm
10
Sorting
Sorting
Hauser Hanley
Hong Haskell
Hsu Hauser
Hayes Hayes
Haskell Hong
Hanley Hornet
Hornet Hsu
12
Insertion Sort
13
Insertion Sort
Insertion sort.
Brute-force sorting solution.
Move left-to-right through array.
Exchange next element with larger elements to its left, one-by-one.
14
Insertion Sort
Insertion sort.
Brute-force sorting solution.
Move left-to-right through array.
Exchange next element with larger elements to its left, one-by-one.
15
Insertion Sort: Java Implementation
16
Insertion Sort: Empirical Analysis
Random: ~ N 2 / 4.
Ascending: ~ N.
10000000
Descendng
100000 Random
Comparsions (millions)
Ascending
1000
10
0.1
0.001
1000 10000 100000 1000000
Input Size
17
Insertion Sort: Mathematical Analysis
E F G H I J D C B A
i
A C D F H J E B I G
18
Sorting Challenge 1
A. Toaster
B. Cell phone
C. Your laptop
D. Supercomputer
E. Google server farm
19
Insertion Sort: Lesson
Comparisons
Computer Thousand Million Billion
Per Second
20
Moore's Law
http://en.wikipedia.org/wiki/Moore's_law
21
Moore's Law and Algorithms
22
Mergesort
23
Mergesort
Mergesort algorithm.
Divide array into two halves.
Recursively sort each half.
Merge two halves to make sorted whole.
24
Mergesort: Example
25
Merging
26
Merging
// copy back
for (int k = 0; k < N; k++) {
a[lo + k] = aux[k];
}
27
Mergesort: Java Implementation
lo mid hi
10 11 12 13 14 15 16 17 18 19
28
Mergesort: Mathematical Analysis
T(N)
N
T(N / 2)
T(N / 2)
2 (N / 2)
T(N / 4)
T(N / 4)
T(N / 4)
T(N / 4)
4 (N / 4)
log2 N
.
T(N / 2k)
.
.
T(2)
T(2)
T(2)
T(2)
T(2)
T(2)
T(2)
T(2)
N / 2 (2)
N log2 N
29
Mergesort: Mathematical Analysis
Mathematical analysis.
analysis comparisons
worst N log2 N
average N log2 N
N actual predicted
30
Sorting Challenge 2
A. Toaster
B. Cell phone
C. Your laptop
D. Supercomputer
E. Google server farm
31
Sorting Challenge 3
32
Mergesort: Lesson
Compares
Computer Insertion Mergesort
Per Second
N = 1 billion
33
Longest Repeated Substring
34
Redundancy Detector
a a c a a g t t t a c a a g c
Brute force.
Try all indices i and j for start of possible match.
Compute longest common prefix for each pair (quadratic+).
a a c a a g t t t a c a a g c
i
j
35
LRS Application: The Shape of a Song
Like a Prayer
http://www.bewitched.com 36
Longest Repeated Substring: Brute-Force Solution
a a c a a g t t t a c a a g c
Brute force.
Try all indices i and j for start of possible match.
Compute longest common prefix (LCP) for each pair.
a a c a a g t t t a c a a g c
i
j
Mathematical analysis.
All pairs: 0 + 1 + 2 + + N-1 ~ N2/2 calls on LCP.
37
Longest Repeated Substring: A Sorting Solution
form suffixes
sort suffixes to bring repeated substrings together
38
Longest Repeated Substring: Java Implementation
int N = s.length();
String[] suffixes = new String[N];
for (int i = 0; i < N; i++)
suffixes[i] = s.substring(i, N);
Arrays.sort(suffixes);
String representation.
A String is an address and a length.
does not copy chars
Characters can be shared among strings.
substring() computes address and length.
D0 D1 D2 D3 D4 D5 D6 D7 D8 D9 DA DB DC DD DE A0 A1
s
a a c a a g t t t a c a a g c D0 15
t = s.substring(5, 15); B0 B1
t
D5 10
Consequences.
substring() is constant-time operation (instead of linear).
Creating suffixes takes linear space (instead of quadratic).
Running time of LRS is dominated by the string sort.
40
Sorting Challenge 4
41
Longest Repeated Substring: Empirical Analysis
estimated
42
Summary
43