TAILIEUCHUNG - Lecture Algorithms and data structures (CSC112) - Chapter 3

Lecture Algorithms and data structures: Chapter 3 - Recursion. This chapter will provide a brief overview of C++. In this topic we will see: The similarities between C# and C++; some differences (including global variables and functions; the preprocessor, compilation, namespaces; printing), concluding with classes, templates, pointers memory allocation and deallocation. | Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search Algorithms for Binary Search 1 Recursion 2 Introduction to Recursion Recursive Definition Recursive Algorithms Finding a Recursive Solution Example Recursive Function Recursive Programming Rules for Recursive Function Example Tower of Hanoi Other examples Introduction Any function can call another function A function can even call itself When a function call itself, it is making a recursive call Recursive Call A function call in which the function being called is the same as the one making the call Recursion is a powerful technique that can be used in place of iteration(looping) Recursion Recursion is a programming technique in which functions call themselves. 3 3 Recursive Definition 4 A definition in which something is defined in terms of smaller versions of itself. To do recursion we should know the followings Base Case: The case for which the solution can be stated non-recursively The case for which the answer is explicitly known. General Case: The case for which the solution is expressed in smaller version of itself. Also known as recursive case Recursive Algorithm 5 Definition An algorithm that calls itself Approach Solve small problem directly Simplify large problem into 1 or more smaller sub problem(s) & solve recursively Calculate solution from solution(s) for sub problem Finding a Recursive Solution 6 a recursive solution to a problem must be written carefully the idea is for each successive recursive call to bring you one step closer to a situation in which the problem can easily be solved this easily solved situation is called the base case each recursive algorithm must have at least one base case, as well as a general (recursive) case Example 1 7 We can write a function called power that calculates the result of raising an integer to a positive power. If X is an integer and N is a positive integer, .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.