tailieunhanh - Lecture Introduction to computing systems (2/e): Chapter 16 - Yale N. Patt, Sanjay J. Patel

Chapter 16 - Recursion. The main contents of this chapter include all of the following: What is recursion? recursion versus iteration, towers of Hanoi, fibonacci numbers, binary search, integer to ASCII,. | Chapter 16 Recursion Mathematical Definition: RunningSum(1) = 1 RunningSum(n) = n + RunningSum(n-1) Recursive Function: int RunningSum(int n) { if (n == 1) return 1; else return n + RunningSum(n-1); } What is Recursion? A recursive function is one that solves its task by calling itself on smaller pieces of data. Similar to recurrence function in mathematics. Like iteration -- can be used interchangeably; sometimes recursion results in a simpler solution. Example: Running sum ( ) 16- Executing RunningSum RunningSum(4) RunningSum(3) RunningSum(2) RunningSum(1) return value = 1 return value = 3 return value = 6 return value = 10 return 1; return 2 + RunningSum(1); return 3 + RunningSum(2); return 4 + RunningSum(3); res = RunningSum(4); 16- High-Level Example: Binary Search Given a sorted set of exams, in alphabetical order, find the exam for a particular student. 1. Look at the exam halfway through the pile. 2. If it matches the name, we're done; if it does not match, then. 3a. If the name is greater (alphabetically), then search the upper half of the stack. 3b. If the name is less than the halfway point, then search the lower half of the stack. 16- Binary Search: Pseudocode Pseudocode is a way to describe algorithms without completely coding them in C. FindExam(studentName, start, end) { halfwayPoint = (end + start)/2; if (end 16- High-Level Example: Towers of Hanoi Task: Move all disks from current post to another post. Rules: (1) Can only move one disk at a time. (2) A larger disk can never be placed on top of a smaller disk. (3) May use third post for temporary storage. Post 1 Post 2 Post | Chapter 16 Recursion Mathematical Definition: RunningSum(1) = 1 RunningSum(n) = n + RunningSum(n-1) Recursive Function: int RunningSum(int n) { if (n == 1) return 1; else return n + RunningSum(n-1); } What is Recursion? A recursive function is one that solves its task by calling itself on smaller pieces of data. Similar to recurrence function in mathematics. Like iteration -- can be used interchangeably; sometimes recursion results in a simpler solution. Example: Running sum ( ) 16- Executing RunningSum RunningSum(4) RunningSum(3) RunningSum(2) RunningSum(1) return value = 1 return value = 3 return value = 6 return value = 10 return 1; return 2 + RunningSum(1); return 3 + RunningSum(2); return 4 + RunningSum(3); res = RunningSum(4); 16- High-Level Example: Binary Search Given a sorted set of exams, in alphabetical order, find the exam for a particular student. 1. Look at the exam halfway through the pile. 2. If it matches the name, we're done; if it does not match, .

TÀI LIỆU MỚI ĐĂNG
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.