1 ENERGY 211 / CME 211 Lecture 13 October 20, 2008.

Slides:



Advertisements
Similar presentations
Back to Sorting – More efficient sorting algorithms.
Advertisements

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
CS50 SECTION: WEEK 3 Kenny Yu. Announcements  Watch Problem Set 3’s walkthrough online if you are having trouble.  Problem Set 1’s feedback have been.
Factorial Recursion stack Binary Search Towers of Hanoi
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. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Recursion. Binary search example postponed to end of lecture.
Recursion Chapter 11 Chapter 11.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 16: Recursion.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Merge sort, Insertion sort
Recursion.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort or bubble sort 1. Find the minimum value in the list 2. Swap it with the value.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 14 Recursion.
MergeSort Source: Gibbs & Tamassia. 2 MergeSort MergeSort is a divide and conquer method of sorting.
Recursion In general there are two approaches to writing repetitive algorithms. One uses loops(while, do while and for): the other uses recursion. Recursion.
Recursion.
Recursion CITS1001.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Debugging applications, using properties Jim Warren – COMPSCI 280 S Enterprise Software Development.
Warmup Write a function to add two integer parameters and return the result.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Lecture 10 Recursion CSE225: Data Structures. 2 A Look Back at Functions #include double distance(double x1, double y1, double x2, double y2) { double.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 17: Recursion.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
1 Debugging: Catching Bugs ( II ) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 14 Recursion.
CSE 332: C++ debugging Why Debug a Program? When your program crashes –Finding out where it crashed –Examining program memory at that point When a bug.
Debugging Xin Tong. GDB GNU Project debugger Allows you to see what is going on `inside' another program while it executes or crashed. (Faster than printing.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 11 – gdb and Debugging.
Chapter 6 Recursion. Solving simple problems Iteration can be replaced by a recursive function Recursion is the process of a function calling itself.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Lecture #3 Analysis of Recursive Algorithms
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
1 ENERGY 211 / CME 211 Lecture 14 October 22, 2008.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Chapter 15 Recursion.
Chapter 15 Recursion.
CS 162 Intro to Programming II
Sorting by Tammy Bailey
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
Unit-2 Divide and Conquer
24 Searching and Sorting.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 17: Recursion.
Recursion Chapter 11.
Debugging.
Presentation transcript:

1 ENERGY 211 / CME 211 Lecture 13 October 20, 2008

2 Recursion A recursive function is a function that, directly or indirectly, calls itself Each function call has its own copy of parameters and local variables Internally, function calls are maintained using a stack Make sure that recursion will end!

3 Example: Factorial int factorial(int n) { if (n==0) return 1; return n*factorial(n–1); } This will compute n! for any nonnegative integer n What is wrong with this function?

4 Applications of Recursion The factorial example is a simple illustration of recursion, but it can easily be implemented non-recursively Recursion useful whenever an algorithm solves a problem by applying itself to “smaller” instances of the problem Often, algorithms are more rapidly designed recursively, which encourages rapid application development

5 Mergesort Simplistic algorithms for sorting an array of n elements take O(n 2 ) comparisons In mergesort, the array is divided in half, and each half is sorted individually The two sorted halves are then merged in O(n) comparisons, for O(n log 2 n) comparisons overall Limiting case of recursion: an array with one element is already sorted!

6 Fast Fourier Transform The Fast Fourier Transform (FFT) computes the discrete Fourier transform of a vector of functions values It recursively transforms odd-numbered and even-numbered points, and then merges the results O(n 2 ) work reduced to O(n log 2 n) For efficiency, should use pointers that take larger and larger steps to access elements!

7 Debuggers A debugger is a tool that performs step- by-step execution of a program, allowing the programmer to study its behavior and find bugs IDEs, like MDS and Microsoft Visual Studio, typically include debuggers A debugger runs your program, stopping execution at user-specified breakpoints to allow inspection

8 The MDS Debugger Before debugging, use F9 to set breakpoints in your program F5 begins execution within the debugger, or resumes after breakpoint When stopped, F10 steps through next line F11 steps into function call Shift-F11 steps out of function Hover over variables to see values

9 The gdb Debugger gdb is the GNU debugger Run from UNIX prompt with executable filename as argument break command sets breakpoint, can specify function name or line number run command begins execution step command advances one line, steps into functions automatically cont resumes execution print shows value of given expression

10 Next Time Testing strategies Debugging strategies Designing software with testing and debugging in mind!