COMP 110-001 Recursion, Searching, and Selection Yi Hong June 12, 2015.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Garfield AP Computer Science
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
8 Algorithms Foundations of Computer Science ã Cengage Learning.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
Computation for Physics 計算物理概論 Algorithm. An algorithm is a step-by-step procedure for calculations. Algorithms are used for calculation, data processing,
Recursion Chapter 11 Chapter 11.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Algorithm Efficiency and Sorting
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
1 Algorithmic analysis Introduction. This handout tells you what you are responsible for concerning the analysis of algorithms You are responsible for:
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
CS1101: Programming Methodology Aaron Tan.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
ITEC 2620A Introduction to Data Structures
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
1 Searching and Sorting Linear Search Binary Search.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
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.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
CSC 211 Data Structures Lecture 13
CSCI 51 Introduction to Programming March 12, 2009.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
New Mexico Computer Science For All Search Algorithms Maureen Psaila-Dombrowski.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Spring 2006CISC101 - Prof. McLeod1 Announcements Assn 4 is posted. Note that due date is the 12 th (Monday) at 7pm. (Last assignment!) Final Exam on June.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
12. Searching/Sorting Programming in C++ Computer Science Dept Va Tech August, 2000 © Barnette ND, McQuain WD, Keenan MA 1 Simple Searching Many.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Searching and Sorting Searching algorithms with simple arrays
Chapter 15 Recursion.
Decrease-and-Conquer Approach
Searching Given a collection and an element (key) to find… Output
Introduction to Search Algorithms
Chapter 15 Recursion.
Searching.
Searching CSCE 121 J. Michael Moore.
Searching and Sorting Arrays
ITEC 2620M Introduction to Data Structures
And now for something completely different . . .
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Presentation transcript:

COMP Recursion, Searching, and Selection Yi Hong June 12, 2015

Announcements  Homework 4 deadline extended to June 13 th, by 11:59pm  Final exam, comprehensive Wednesday, June 17 th, 8am – 11am Review on Monday

Today  Introduction to Recursion  Introduction to Search & Selection Not the focus of the final exam But, you should be able to understand the code in the slides (and know how to use the code in similar problems by making slight modifications).

Recursion  Whenever an algorithm has one subtask that is a smaller version of the entire algorithm’s task, it is said to be recursive  Recursion: you write a method to solve a big task, and the method invokes itself to solve a smaller subtask E.g., I want to eat 5 apples now. My subtask can be eating 4 apples, eating 3 apples, eating 2 apples, et….. To eat 5 apples, I can do: Eat 3 apples + Eat 2 apples Eat 1 apple + Eat 4 apples Eat 1 apple

Recursion  Eating 1 apple is the smallest task that I can have. I cannot divide it anymore.  This is the base case in recursion.  Recursion is to divide a big task into smaller tasks. Smaller tasks are then divided further. Until we reach base case.

Recursion  Let’s start with a simple example: calculating factorial Factorial(n) = n * (n-1) * (n-2) * ….. * 3 * 2 * 1  How do you solve a task with smaller task(s)? Factorial(n) = n * Factorial(n-1)

Recursion  Translate this into Java code public static int factorial( int n ) { if (n==1) return 1; // base case else return n * factorial(n-1); }

Recursion  The recursion form can be more natural in many problems (than using loops)  Some problems can be hard to formulate using naïve looping (but such problems are beyond the scope of this course)  Let’s see more recursion examples: Digits to Words from textbook

Recursion: Digits to Words  Define a method that takes a single integer as an argument and displays the digits of that integer as words. For example, if the argument is the number 223, the method should display: two two three  Base case?  Recursive rule?

Recursion: Digits to Words  Base case: only 1 digit print word for 1 digit  Recursive rule: Print words for n digits --> (print words for first n-1 digits) + (print word for last digit)

Recursion: Digits to Words public static void displayAsWords( int number ) { if (number < 10) // base case System.out.print(getWordFromDigit(number) + " "); else //number has two or more digits { displayAsWords(number / 10); System.out.print(getWordFromDigit(number % 10) + " "); } You should be able to write out: getWordFromDigit(int num)

Recursion: Digits to Words  displayAsWords(987);

Search  Given a list of numbers (in an array), how do you search for a number? Return index if the number is found in the array Return -1 if the number is not found

Sequential (Linear) Search  Basic idea For each item in the list: if that item has the desired value, stop the search and return the item's location. Return Not Found.  Can you do better than this (by making it faster)?  The general answer is no No assumptions made on array (unsorted) In worst case, have to examine each array element at least once

Search  How about sorted array? ( numbers are in ascending or descending order )  Can you make the linear search faster?

Search  Let’s see an example: searching for 76

Search  Given n numbers: In linear search, you need to explore one possible choice in each iteration Worst case, n comparisons needed With the new search algorithm (which only works for sorted array), we can reduce half of the search space in each iteration! How many comparisons do I need in the worst case?

Binary Search int binary_search(int A[], int key, int imin, int imax) { // test if search range is empty if (imax < imin) { return KEY_NOT_FOUND; // set is empty } else { // calculate midpoint to cut set in half int imid = midpoint(imin, imax); // three-way comparison if (A[imid] > key) // key is in lower subset return binary_search(A, key, imin, imid-1); else if (A[imid] < key) // key is in upper subset return binary_search(A, key, imid+1, imax); else // key has been found return imid; }

Search Algorithms  A lot of search algorithms, here we just covered two simplest cases: Linear search in a list (array) of numbers Binary search in sorted array  More with different data structures: Search in graphs and trees (computer science concepts, not the usual graph/tree) E.g., search for a move in chess game Search for relations/patterns in social network communication graph

Selection  One selection problem: Find the smallest / largest number in a given list (array) No assumption made on the list ( so it is not sorted )  We have solved this in lab 4 Loop through each element, keep the largest/smallest  Let’s relax the problem a bit

Selection  Find the k-th smallest (or largest) element in a list of numbers  How to solve this problem? Go through each element, for each element, check its position in list How many operations in the worst case? Sort array first. Then get the k-th element How many operations in the worst case

Selection Quickselect (quick in practice, but not in the worst case)  To find k-th smallest number in n numbers: Randomly pick a number from the list, call it p Partition the array into two parts: Numbers that are < p (m numbers) Numbers that are > p (n – m – 1 numbers) If m==k-1, p is the k-th smallest If m > k, find the k-th smallest in the m numbers If m < k, find the (k-m-1)-th smallest in the (n-m-1) numbers  On average, this requires ~n*constant operations  But in the worst case, it is ~n^2*constant

Next Class  Introduction to sorting