Download presentation
Presentation is loading. Please wait.
1
Programming Abstractions
CS106B Cynthia Lee
2
Fractals: Boxy Snowflake Fractal
Fractals, squee!!!
3
Boxy Snowflake example
const double SCALE = 0.45; void drawFractal(GWindow& w, double cx, double cy, double dim, int order) { if (order == 0) return; drawFractal(window, cx-dim/2, cy-dim/2, SCALE*dim, order-1); drawFractal(window, cx+dim/2, cy+dim/2, SCALE*dim, order-1); drawFractal(w, cx-dim/2, cy+dim/2, SCALE*dim, order-1); drawFractal(window, cx+dim/2, cy-dim/2, SCALE*dim, order-1); } Where should this line of code be inserted to produce the pattern shown on the right? drawFilledBox(w, cx, cy, dim, "Gray", "Black"); (A) Insert code here (B) Insert code here (C) Insert code here (D) Insert code here (E) None of the above
4
Variants: How can we code this?
5
Real or Photoshop? Can these be made by changing the order of lines and/or deleting lines in the draw() function? (1) (2) (A) Only #1 is real (B) Only #2 is real (C) Both are ‘shopped (D) Both are real
6
Classic and important CS problem: searching
7
Current issue in computer science: we have loads of data
Current issue in computer science: we have loads of data! Once we have all this data, how do we find anything?
8
Imagine storing sorted data in an array
How long does it take us to find a number we are looking for? 1 2 3 4 5 6 7 8 9 10 13 25 29 33 51 89 90 95
9
Imagine storing sorted data in an array
How long does it take us to find a number we are looking for? If you start at the front and proceed forward, each item you examine rules out 1 item 1 2 3 4 5 6 7 8 9 10 13 25 29 33 51 89 90 95
10
Imagine storing sorted data in an array
1 2 3 4 5 6 7 8 9 10 13 25 29 33 51 89 90 95 If instead we jump right to the middle, one of three things can happen: The middle one happens to be the number we were looking for, yay! We realize we went too far We realize we didn’t go far enough
11
Imagine storing sorted data in an array
1 2 3 4 5 6 7 8 9 10 13 25 29 33 51 89 90 95 If instead we jump right to the middle, one of three things can happen: The middle one happens to be the number we were looking for, yay! We realize we went too far We realize we didn’t go far enough Ruling out HALF the options in one step is so much faster than only ruling out one!
12
Binary search 1 2 3 4 5 6 7 8 9 10 13 25 29 33 51 89 90 95 Let’s say the answer was case 3, “we didn’t go far enough” We ruled out the entire first half, and now only have the second half to search We could start at the front of the second half and proceed forward…
13
Binary search 1 2 3 4 5 6 7 8 9 10 13 25 29 33 51 89 90 95 Let’s say the answer was case 3, “we didn’t go far enough” We ruled out the entire first half, and now only have the second half to search We could start at the front of the second half and proceed forward…but why do that when we know we have a better way? Jump right to the middle of the region to search
14
Binary search 1 2 3 4 5 6 7 8 9 10 13 25 29 33 51 89 90 95 Let’s say the answer was case 3, “we didn’t go far enough” We ruled out the entire first half, and now only have the second half to search We could start at the front of the second half and proceed forward…but why do that when we know we have a better way? Jump right to the middle of the region to search RECURSION!!
15
Designing a recursive algorithm
Recursion is a way of taking a big problem and repeatedly breaking it into smaller and smaller pieces until it is so small that it can be so easily solved that it almost doesn't even need solving. There are two parts of a recursive algorithm: base case: where we identify that the problem is so small that we trivially solve it and return that result recursive case: where we see that the problem is still a bit too big for our taste, so we chop it into smaller bits and call our self (the function we are in now) on the smaller bits to find out the answer to the problem we face
16
Binary Search bool binarySearch(const Vector<int>& data, int key){ return binarySearch(data, key, 0, data.size() - 1); } bool binarySearch(const Vector<int>& data, int key, int start, int end){ if (start > end) return false; int mid = (start + end) / 2; if (key == data[mid]) { return true; } else if (key < data[mid]) { return binarySearch(data, key, ______, ______); } else {
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.