The Self-Taught Computer Scientist


Download 1.48 Mb.
Pdf ko'rish
bet28/147
Sana17.06.2023
Hajmi1.48 Mb.
#1540634
1   ...   24   25   26   27   28   29   30   31   ...   147
Bog'liq
books-library.net-11301817Az7X6

quadratic time: An algorithm runs in quadratic time when its performance is directly proportional 
to the square of the size of the problem.
cubic time: An algorithm runs in cubic time when its performance is directly proportional to the 
cube of the size of the problem.
polynomial time: An algorithm runs in polynomial time when it scales as O(
n**a), where a = 2 for 
quadratic time and 
a = 3 for cubic time.
exponential time: An algorithm runs in exponential time when it contains a constant raised to the 
problem’s size.
brute- force algorithm: A type of algorithm that tests every possible option.


Introduction to Algorithms
18
best- case complexity: How an algorithm performs with ideal input.
worst- case complexity: How an algorithm performs in the worst possible scenario for it.
average- case complexity: How an algorithm performs on average.
space complexity: The amount of memory space an algorithm needs.
fixed space: The amount of memory a program requires.
data structure space: The amount of memory a program requires to store the data set.
temporary space: The amount of memory an algorithm needs for intermediary processing, for 
example, if your algorithm needs to temporarily copy a list to transfer data.
Challenge
1. 
Find a program you’ve written in the past. Go through it and write down the time complexities 
for the different algorithms in it.


2
Recursion
To understand recursion, one must first understand recursion.
Anonymous
An iterative algorithm solves problems by repeating steps over and over, typically using a loop. Most 
of the algorithms you’ve written in your programming journey so far are likely iterative algorithms. 
Recursion is a method of problem- solving where you solve smaller instances of the problem until you 
arrive at a solution. Recursive algorithms rely on functions that call themselves. Any problem you 
can solve with an iterative algorithm, you can also solve with a recursive one; however, sometimes, a 
recursive algorithm is a more elegant solution.
You write a recursive algorithm inside of a function or method that calls itself. The code inside the 
function changes the input and passes in a new, different input the next time the function calls itself. 
Because of this, the function must have a base case: a condition that ends a recursive algorithm to 
stop it from continuing forever. Each time the function calls itself, it moves closer to the base case. 
Eventually, the base case condition is satisfied, the problem is solved, and the function stops calling 
itself. An algorithm that follows these rules satisfies the three laws of recursion:


A recursive algorithm must have a base case.


A recursive algorithm must change its state and move toward the base case.


A recursive algorithm must call itself recursively.
To help you understand how a recursive algorithm works, let’s take a look at finding the factorial of 
a number using both a recursive and iterative algorithm. The factorial of a number is the product of all 
positive integers less than or equal to the number. For example, the factorial of 5 is 5 × 4 × 3 × 2 × 1.
5! = 5 * 4 * 3 * 2 * 1
Here is an iterative algorithm that calculates the factorial of a number
n
:
def factorial(n):



Download 1.48 Mb.

Do'stlaringiz bilan baham:
1   ...   24   25   26   27   28   29   30   31   ...   147




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling