Search and Recursion pt. 2 CS221 – 2/25/09. How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

MATH 224 – Discrete Mathematics
More on Recursion More techniques 1. Binary search algorithm Binary searching for a key in an array is similar to looking for a word in dictionary Take.
Sorting Part 4 CS221 – 3/25/09. Sort Matrix NameWorst Time Complexity Average Time Complexity Best Time Complexity Worst Space (Auxiliary) Selection SortO(n^2)
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion. Binary search example postponed to end of lecture.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Chapter 19 Recursion.
16/23/2015 9:48 AM6/23/2015 9:48 AM6/23/2015 9:48 AMRecursion Recursion Recursion is when a function calls itself to implement an algorithm. Really a paradigm.
Recursion.
Searching Arrays Linear search Binary search small arrays
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Recursion.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 12 l Basics of Recursion l Programming with Recursion Recursion.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Chapter 11- Recursion. Overview n What is recursion? n Basics of a recursive function. n Understanding recursive functions. n Other details of recursion.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
Recursion Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment? zWhat is a recursive function?
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
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.
CSC 211 Data Structures Lecture 13
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
CS Class 22 Today  A word from the Real World What happens when software goes bad…  Binary Search Announcements  Exam 3 – Nov. 25 th in class.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 6 Recursion. Solving simple problems Iteration can be replaced by a recursive function Recursion is the process of a function calling itself.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Chapter 19: Recursion.
Recursion Version 1.0.
OBJECT ORIENTED PROGRAMMING II LECTURE 23 GEORGE KOUTSOGIANNAKIS
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Searching: linear & binary
CSE 373 Data Structures and Algorithms
Searching.
Last Class We Covered Recursion Stacks Parts of a recursive function:
Presentation transcript:

Search and Recursion pt. 2 CS221 – 2/25/09

How to Implement Binary Search Take a sorted data-set to search and a key to search for Start at the mid-point and see if that’s the key If not, see if you need to search above or below the mid-point Pick halfway point above or below and test again Repeat until you can no longer cut the remaining set in half

Binary Search

How to Implement Binary Search Given a sorted array and a key First = start of list Mid = middle of list Last = end of list if array[mid] == key – Return key If mid > key – Last = mid - 1 If mid < key – First = mid + 1 Mid = (first + last)/2 Continue until first > last If the key isn’t found, throw an exception

Integer Binary Search

Generic Binary Search

Recursion

A recursive function is a function that calls itself Any iterative algorithm can be written recursively Any recursive algorithm can be written iteratively That doesn’t mean that you should!

Recursion Benefits Some algorithms naturally lend themselves to recursion – Easier to understand – Easier to implement – Easier to maintain

Recursion Drawbacks Each recursive call goes on the stack. Recursion requires more memory Recursion can result in a stack overflow exception Recursion is marginally slower – has more overhead Recursion can be harder to understand, implement, and maintain

When should you use recursion? When it makes your solution simpler! – The solution can be described as a process that breaks the problem into smaller pieces and then that process is applied to each smaller piece The only benefit of recursion is simplicity – If it feels too hard or is adding complexity, stop using it.

Simple Recursive Function Test() { //Call itself Test(); }

Simple Recursive Function

Infinite Recursion Recursive functions must have an exit condition If not – Boom!

Stack Overflow

Exit Condition Test(int i) { i++; if (i <= 10) { Test(i); }

How to Design a Recursive Algorithm Determine the base case and base solution Determine how to break the problem up and provide recursive logic Determine how to combine into a cohesive solution

Recursive Linear Search Base: – Item we are looking at matches the key – We are outside of the array bounds Recursive Logic: – If base case doesn’t match, we need to look at each item in turn Combine: – Once we’ve found the item we can return from all recursive calls

Recursive Linear Search Take an array to search, an index to start with, a key to search for If index is outside array bounds, throw exception If array[index] == key, return array [index] Else call Linear Search recursively (array, index+1, key)

Recursive Linear Search

Tail Recursion This is an example of tail recursion: – There is a single recursive call – It is the last line of the function Tail recursion is relatively simple More complicated: – You can have multiple recursive calls – You can have additional code following the recursive call

What if this Wasn’t Tail Recursion?

Recursive Binary Search Base: – We’ve found the item we are looking for – If First > Last then throw an exception Recursive Logic: – If key > item then partition higher – If key < item then partition lower Combine: – Once we’ve found the item we can return from all recursive calls

Recursive Binary Search Take an array to search, a key to search for, first and last boundaries If first > last, throw an exception Find the mid point If array[mid] == key, return array[mid] Else if array[mid] < key call Binary Search recursively where first = mid + 1 Else if array[mid] > key call Binary Search recursively where last = mid – 1

Recursive Binary Search

Recursive Factorial Example (n=5): 5*4*3*2*1 = 120 Base: – If n == 0 the return 1 – If n < 0 then throw an exception Recursive Logic: – Recursively call factorial with n – 1 Combine: – Multiply n * result of each recursive call

Recursive Factorial

Recursive Fibonacci Example (n=7): 0+0 = 0, 0+1 = 1, 1+0 = 1, 1+1 = 2, 2+1 = 3, 3+2 = 5, 5+3 = 8, 8+5 = 13 Base: – If n == 0, then return 0 – If n == 1, then return 1 Recursive Logic: – Recursively call fibonacci with n - 1 – Recursively call fibonacci with n - 2 Combine: – Return the sum of the results

Recursive Fibonacci

Previous solution is easy to understand and implement Unfortunately it is O(2^n) The text has a better solution that is O(n)

Recursive Power Example (x = 2, n = 5): 2*2*2*2*2 = 32 Base: – If n = 0 then return 1 – If n < 0 then throw an exception Recursive Logic: – Recursively call power with n - 1 Combine: – Multiply x * the result of the recursive call

Recursive Power