Recursion

¡Supera tus tareas y exámenes ahora con Quizwiz!

Recursive selection sort

-*Base case*- occurs when our smaller array of larger values has only *one value* -We can recursively sort the smaller arrays by swapping the *smallest value* in our selection into the i position of the array, and then plugging values *back into function* where i = i+1 and the size = size-1 -This will continue until size = 1 -Basically we are replacing the *for loop*

Recursive bubble sort

-*Base case*- occurs when there is only *one value left* in "bubbled down" array, then it will just return what is in the array -You're using the recursion to tell the function to run again when for the next element up in your matrix

Recursive insertion sort

-*base case*- occurs when small array not being sorted has only *1 value* -Will still need one for loop to sort the elements in our selected array -Recursion comes into play when we want to prompt function to *sort the next array* that will have size of size-1 -We tell the function to keep on taking itself until the size=1

Recursive binary search

-Basically you are telling the program to *run again* if it has no yet found the value or proven it doesn't exist -*Two base cases*- it will not find the value if left>right, or it will find the value and it will be at the middle

Recursion example- Factorial function

-Classic example of how a recursive function works -We are defining the function *in terms of itself* and then we define a simple version -*Base case*- we have factorial 1 which returns 1xfactorial(0) which will equal 1 -Will keep on diving back into the function until it finishes, there will not be just one n value

What is recursion

-In some languages we have to use what is called a *recursive method* to solve problems -We will want to write a function that will *call itself* in the function -Assume that the computer can solve smaller problems in smaller instances -We are *breaking up* the problem into a more general problem

Divide and conquer approach

-Split each problem in *half* -Split the problem into *smaller more simpler subproblems* -Recursively solve the two subproblems -Combine the solutions to create one solution

Recursive Sum of Squares- computing the sum of squares between two numbers, m and n

-We use what is called a *divide and conquer* approach -This function will run for this *base case* and continue to run until m=n

How to solve problems recursively?

1) Write *if else statements*- we will need *at least 2* statements -One statement will be the *recursive case* where the function *calls itself* -The other statement will be the *base case* where the method doesn't call itself 2) The base case- will be the *simplest case* 3) Write recursive call- on the *next simplest case*, with the function in it -The less simple cases must always return to the base case! -Identify *smaller problem*- we must think to the smallest, simplest version of the problem that we can solve directly, this is called the *base case*


Conjuntos de estudio relacionados

MEDSURG II: Saunders End of Life Care

View Set

Roaring Economy to Great Depression QUIZ 100%

View Set

Virginia Real Estate License Exam

View Set

Service Titan Essential System Practice

View Set

Chapter 12 - Individual Policy Provisions

View Set

Ancient civilizations: Greece: dark ages

View Set