Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Class notes

Summary of class notes

Rating
-
Sold
-
Pages
6
Uploaded on
08-11-2024
Written in
2024/2025

These notes are in-class notes with added information like pictures and diagrams taken from the net. They summarize and display the most important parts of these 3 chapters. They are color-coordinated for ease of learning and structured with enough white space that if/when printed, one can annotate them to add further information they deem necessary for their comprehension. These are simple notes without overwhelming colors so one may make it their own.

Show more Read less
Institution
Course

Content preview

Lecture 1 Recursion

Recursion: Solving a problem by breaking it down into smaller, similar problems.
Base Case: A specific condition that stops the recursion and provides a direct
solution. This is the point where the program stops calling itself and returns a
result.
Recursive Programming: Writing methods that call themselves to solve problems
recursively. It can be used as an alternative to iterative
approaches.
Example: Factorial
public int factorial(int num){
if (num==0) {
return 1;
else
return num * factorial(num-1);
}
Using FOR LOOP
public int factorial(int num) {
int result = 1;


for (int i = 1; i <= num; i++) {
result *= i;
}
return result;
}

, Difference between Iterative and Recursive


Both iteration and recursion are based on a control structure:
• Iteration uses a repetition structure, to repeat actions (e.g., for or while loop).
• Recursion uses a selection structure, repeating through method calls (e.g., if-
else statement).
Both involve repetition:
• Iteration explicitly uses a repetition structure.
• Recursion achieves repetition through repeated method calls.
Each also has a termination test:
• Iteration terminates when the loop-continuation condition fails.
• Recursion terminates when a base case is recognized (stops when it reaches
a base case).
Both can occur infinitely:
• An infinite loop occurs with iteration if the loop-continuation test never
becomes false.
• Infinite recursion occurs if the recursion step does not reduce the problem
toward the base case.
Recursion repeatedly invokes the mechanism and overhead of method calls, which
can be costly in terms of both processor time and memory space. [ Recursion,
however, has the added overhead of method calls, which can consume more time
and memory compared to iteration.]
In simpler terms:

Infinite loops or recursion can happen if the termination condition is never met.

- For loops, this means the loop condition never becomes false.

- For recursion, this means the base case is never reached.

Written for

Institution
Course

Document information

Uploaded on
November 8, 2024
Number of pages
6
Written in
2024/2025
Type
Class notes
Professor(s)
Gokhool
Contains
Data structures and algorithms

Subjects

$7.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
Elitish

Get to know the seller

Seller avatar
Elitish university of mauritius
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
1 year
Number of followers
0
Documents
3
Last sold
-
Elitish's Summary notes

Summary notes on all topics related to Computer Science/Software Engineering. Simple, no overwhelming colors and examples. These can help refresh your memory or serve as the base for your learning.

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions