week 3 - cs50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · week 3. 1 divide and conquer. 2...
TRANSCRIPT
![Page 1: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/1.jpg)
0
Computer Science 50Introduction to Computer Science I
Harvard College
David J. [email protected]
Week 3
![Page 2: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/2.jpg)
1
Divide and Conquer
![Page 3: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/3.jpg)
2
Parallel Processing
1) Stand up.2) Think to yourself “I am #1”.3) Pair up with someone; add your numbers
together; take that sum as your new number.4) One of you should sit down.5) GOTO step 3 if still standing.
![Page 4: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/4.jpg)
3
Running TimeT (n)
0
2
4
6
8
10
12
14
16
18
20
0 20 40 60 80 100 120
n
T(n)
nlog nn/2
![Page 5: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/5.jpg)
4
Running TimeT (n)
Figure from C++: An Introduction to Data Structures, by Larry Nyhoff.
![Page 6: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/6.jpg)
5
Running TimeT (n)
Figures from C++: An Introduction to Data Structures, by Larry Nyhoff.
![Page 7: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/7.jpg)
6
Asymptotic NotationInformally
OΘΩ
![Page 8: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/8.jpg)
7
Asymptotic NotationFormally
T (n) ∈ O (f (n))
We say that the running time, T (n), of an algorithm is “in big O of f of n” iff there exist an integer n0 > 0 and a real number c > 0 such that T (n) ≤ c · f (n) for all n ≥ n0.
T (n) ∈ Θ (f (n))
We say that the running time, T (n), of an algorithm is “in theta of f of n” iff there exist an integer n0 and real numbers c1, c2 > 0 such that c1 · f (n) ≤ T (n) ≤ c2 · f (n) for all n ≥ n0.
T (n) ∈ Ω (f (n))
We say that the running time, T (n), of an algorithm is “in omega of f of n” iff there exist an integer n0 and a real number c > 0 such that T (n) ≥ c · f (n) for all n ≥ n0.
![Page 9: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/9.jpg)
8
OIn English
O(1) “constant”O(log n) “logarithmic”O(n) “linear”O(n log n) “supralinear”O(n2) “quadratic”O(nc) “polynomial”O(cn) “exponential”O(n!) “factorial”
![Page 10: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/10.jpg)
9
Searching
![Page 11: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/11.jpg)
10
Linear SearchPseudocode
On input n:
For each element i:
If i == n:
Return true.
Return false.
![Page 12: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/12.jpg)
11
Binary SearchIterative Pseudocode
On input array[0], ... , array[n – 1] and k:
Let first = 0.
Let last = n – 1.
While first <= last:
Let middle = (first + last ) / 2.
If k < array[middle] then let last = middle – 1.
Else if k > array[middle] then let first = middle + 1.
Else return true.
Return false.
![Page 13: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/13.jpg)
12
Sum LoopingGet it?
int
sigma(int m)
// avoid risk of infinite loop
if (m < 1)
return 0;
// return sum of 1 through m
int sum = 0;
for (int i = 1; i <= m; i++)
sum += i;
return sum;
seesigma1.c
![Page 14: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/14.jpg)
13
Sum RecursionGet it yet?
int
sigma(int m)
// base case
if (m <= 0)
return 0;
// recursive case
else
return (m + sigma(m-1));
seesigma2.c
![Page 15: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/15.jpg)
14
The Stack, RevisitedFrames
![Page 16: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/16.jpg)
15
Binary SearchRecursive Pseudocode
On input array, first, last, and k, define recurse as:
If first > last then return false.
Let middle = (first + last) / 2.
Else if k < array[middle] then
return recurse(array, first, middle – 1, k).
Else if k > array[middle] then
return recurse(array, middle + 1, last, k).
Else return true.
![Page 17: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/17.jpg)
16
Sorting
4 2 6 8 1 3 7 5
![Page 18: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/18.jpg)
17
Bubble SortPseudocode
Repeat n times:
For each element i:
If element i and its neighbor are out of order:
Swap them.
![Page 19: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/19.jpg)
18
Selection SortPseudocode
Let i := 0.
Repeat n times:
Find smallest value, s, between i and list's end, inclusive.
Swap s with value at location i.
Let i := i + 1.
![Page 20: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/20.jpg)
19
SortingVisualization
![Page 21: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/21.jpg)
20
Merge SortPseudocode
On input of n elements:
If n < 2, return.
Else
Sort left half of elements.
Sort right half of elements.
Merge sorted halves.
![Page 22: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/22.jpg)
21
Merge SortPseudocode
T (n) = 0, if n < 2
T (n) = T (n/2) + T(n/2) + O(n), if n > 1
![Page 23: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/23.jpg)
22
Merge SortHow long does it take to sort 16 elements?
T(16) = 2T(8) + 16 T(8) = 2T(4) + 8 T(4) = 2T(2) + 4T(2) = 2T(1) + 2T(1) = 0
![Page 24: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/24.jpg)
23
Merge Sort
T (1) = 0T (2) = 2T (1) + 2 = 0 + 2 = 2T (4) = 2T (2) + 4 = 4 + 4 = 8T (8) = 2T (4) + 8 = 16 + 8 = 24T (16) = 2T (8) + 16 = 48 + 16 = 64
![Page 25: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/25.jpg)
24
SortingVisualization
![Page 26: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/26.jpg)
25
All Sorts of Sorts
Heap SortInsertion Sort
QuicksortRadix SortShell Sort
...
![Page 27: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/27.jpg)
26
TradeoffsSpace, Time, ...
![Page 28: Week 3 - CS50cdn.cs50.net/2008/fall/lectures/3/week3.pdf · Week 3. 1 Divide and Conquer. 2 Parallel Processing 1) Stand up. 2) Think to yourself “I am #1”. 3) Pair up with someone;](https://reader035.vdocuments.us/reader035/viewer/2022071010/5fc87a4c24cac86cde6efb80/html5/thumbnails/28.jpg)
27
Computer Science 50Introduction to Computer Science I
Harvard College
David J. [email protected]
Week 3