1. Definition
Mathematical notation that describes the upper limit of execution time or the use of an algorithm space. It is denoted as or (f (n)) , where f (n) is a function that represents time or space depending on the input size n .
2. Purpose
Algorithms comparison - : Allows you to compare different algorithms and choose the most efficient for a given problem.
Scalability
: It helps predict how an algorithm will behave when the amount of data increases.
-
3. Complexity analysis
Worse case
: Refers to the scenario where the algorithm takes longer or uses more resources. Big or generally refers to this case.
-
Better case and average case : Although they are important, they are used less frequently for the Big O.
-
4. Space vs. Time
Temporary complexity
: It refers to the time it takes for an algorithm to execute. [
- Spatial complexity : refers to the amount of additional memory you use. You can have notations such as or (1)
(constant space) or - (linear space).
Example:
Import Timeit
IMPORT MATPLOTLIB.PYPLOT AS PLT
Import CProfile
# O (1)
Def Constant_time_operation ():
return 42
# O (log n)
Def logarithmic_time_operation (n):
Count = 0
While N> 1:
N // = 2
Count = 1
return count
# Or (n)
def linear_time_operation (n):
Total = 0
for i in range (n):
Total = i
Total return
# O (n log n)
def linear_logarithmic_time_operation (n):
if n
Thank you for reading !!
Apóy me reacting and opinioning.