CP114-Recursion lectures - Recursion Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive

CP114-Recursion lectures - Recursion Recursion Recursion is...

This preview shows page 1 - 7 out of 24 pages.

Recursion
Image of page 1

Subscribe to view the full document.

Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition is one which uses the word or concept being defined in the definition itself To solve a big problem, break it down into smaller problems using recursion (divide and conquer)
Image of page 2
Recursion A recursive algorithm Calls itself on a smaller problem Combines results to get solution Typically, has three components: A base case that is solved without recursion A recursive call Convergence to the base case Examples: Binary Search Factorial Fibonacci number
Image of page 3

Subscribe to view the full document.

Sequential Search as Recursion //Find (the first instance of) element in list If the list is empty, return “not found” Else if the first element of the list is the one you’re looking for, return it Else search in the smaller list starting from the second element of the list
Image of page 4
Sequential Search in C /* Check if value is in array of integers Note: end is one value beyond the last in list */ int search1 (int value, int list[], int st, int end){ if (st==end) return 0 ; /* empty list */ if (list[st]==value) return 1; return search1(value, list, st+1, end); }
Image of page 5

Subscribe to view the full document.

Sequential Search as Divide & Conquer
Image of page 6
Image of page 7

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes