CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching Arrays Linear search Binary search small arrays
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Building Java Programs Chapter 13 Searching reading: 13.3.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Lecture 12. Searching Algorithms and its analysis 1.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee BFS animation slides by Keith Schwarz CS2 in C++ Peer Instruction Materials by Cynthia Bailey.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Programming Abstractions Cynthia Lee CS106X. Topics:  Finish up heap data structure implementation › Enqueue (“bubble up”) › Dequeue (“trickle down”)
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
CSE 20 – Discrete Mathematics Dr. Cynthia Bailey Lee Dr. Shachar Lovett Peer Instruction in Discrete Mathematics by Cynthia Leeis licensed under a Creative.
CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Recursion – some examples. Sum of Squares Write Vertical.
Programming Abstractions Cynthia Lee CS106X. Recursion! The exclamation point isn’t there only because this is so exciting, it also relates to one of.
Searching and Sorting Searching algorithms with simple arrays
Programming Abstractions
CS 106X – Programming Abstractions in C++
Programming Abstractions
Programming Abstractions
Programming Abstractions
Programming Abstractions
searching Concept: Linear search Binary search
Programming Abstractions
Programming Abstractions
CSE 12 – Basic Data Structures
Sorting Algorithms.
Presentation transcript:

CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

CS106X: Today’s Topics Recursion!!! 1. Binary search (continued) 2. Fractals 2

Binary Search (continued)

Binary search  Let’s say the answer was 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

Binary search  Let’s say the answer was 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 !!

To write a recursive function, we need base case(s) and recursive call(s)  What would be a good base case for our Binary Search function? A. Only three items remain: save yourself an unnecessary function call that would trivially divide them into halves of size 1, and just check all three. B. Only two items remain: can’t divide into two halves with a middle, so just check the two. C. Only one item remains: just check it. D. No items remain: obviously we didn’t find it. E. More than one

Binary Search bool binarySearch(Vector & data, int key){ return binarySearch(data, key, 0, data.size()-1); } bool binarySearch(Vector & data, int key, int start, int end){ // Write code to handle the base case “No items remain: // obviously we didn’t find it” by returning false (A) if (data.size() <= 0) return false; (B) if (end < start) return false; (C) if (end == start) return false; (D) Other/none/more //to be continued… }

Binary Search bool binarySearch(Vector & data, int key){ return binarySearch(data, key, 0, data.size()-1); } bool binarySearch(Vector & data, int key, int start, int end){ if (end < start) return false; int mid = (start + end)/2; if (data[mid] == key) return true; else if (data[mid] > key) { (A) return binarySearch(data,key,mid+1,end); (B) return binarySearch(data,key,mid,end-1); (C) return binarySearch(data,key,start,mid-1); (D) Other/none/more } // to be continued… }

Binary Search bool binarySearch(Vector & data, int key){ return binarySearch(data, key, 0, data.size()-1); } bool binarySearch(Vector & data, int key, int start, int end){ if (end < start) return false; int mid = (start + end)/2; if (data[mid] == key) return true; else if (data[mid] > key) return binarySearch(data,key,start,middle-1); else return binarySearch(data,key,middle+1,end); }

Binary Search: arm’s length recursion bool binarySearch(Vector & data, int key){ return binarySearch(data, key, 0, data.size()-1); } bool binarySearch(Vector & data, int key, int start, int end){ if (end < start) return false; if (end == start && data[end] == key) return true; if (start == end-1 && (data[start] == key || data[end] == key)) return true; int mid = (start + end)/2; if (data[mid] == key) return true; else if (data[mid] > key) return binarySearch(data,key,start,middle-1); else return binarySearch(data,key,middle+1,end); }

Boxy Snowflake Fractal

static const double SCALE = 0.45; static void drawFractal(GWindow& window, double cx, double cy, double dim, int order) { if (order >= 0) { 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(window, cx-dim/2, cy-dim/2, SCALE*dim, order-1); drawFractal(window, cx+dim/2, cy+dim/2, SCALE*dim, order-1); } Boxy Snowflake example Where should this line of code be inserted to produce the pattern shown? drawFilledBox(window, cx, cy, dim, "Gray", "Black"); (E) None of the above (A) Insert it here (B) Insert it here (C) Insert it here (D) Insert it here

Variants: How can we code this?

Real or photoshop? Can these be made by changing the order of lines and/or deleting lines in the draw function? (A) 1 is real (B) 2 is real (C) Both are ‘shopped (D) Both are real (1) (2)