Big O Notation
Big O Notation
The speed of an algorithm can change significantly with the size of it's input
for small datasets , a particular algorithm might be faster than than another, but as the dataset increases, the relative
performance can shift.
Comparing algorithms effectifely needs measures that are independent of machine specific charactersitcts,programing
language
Execution Times are highly variable and depend on several factors unrelated to the algorithm's inherent efficiency such as :
Hardware, Background processes so not a good measure.
Number of Statements executed is not a good measure since it varies with the programing language as well as the styple of
the individual programmer
For instance Let us assume that you go to a shop to buy a car and a bicycle. If your friend sees you there and asks what you
are buying, then in general you say buying a car. This is because the cost of the car is high compared to the cost of the bicycle
(approximating the cost of the bicycle to the cost of the car).
We can represent the cost of the car and bicycle in terms of function
and for a given function ignore the lower oder terms that are relatively insignificant
for example in the case , , and are the individual costs of some function and approximate to since is
the highest growth rate
From top to bottom , the functions are ordered from the highest growth rate to the lowest:
(Double exponential growth – extremely fast-growing function)
(Linear growth)
Types of Analysis
This involves understanding with which input the algorithm takes less time and with which inputs it takes long time
Measured in terms of time complexity (how long it takes to run) and space complexity (how much memory it requires)
Relevant for the selection of the most efficent algorithm for a given problem
Importance: it gives an upper bound on the running time, ensuring the algoritm will not perform worse than
Example: In the case of linear search algorithm, the worst case occurs when the element being search is the last
element of the array or is not present at all.
Importance: It provides a lower bound on the running time of an algorithm, representing the most optimistic scenario
Example: In the case of linear search, the best case occurs when the element being search is the first element of the array.
It involves running the algorithm multiple times with various inputs and calculating the average running time
Importance : It offers a more realistic measure of an algorithm's performance, assuming all inputs are equally likely to
occur
Example : For linear search search, if we assume that the search element is equally likely to be at any position in the
array, the average case becomes
Big O Notation
Big O notation is a mathematical concept used in computer science to describe the upper bound of an algorithm's
performance
It gives us an upper limit on the time an algorithm will take to run in terms of the input data.
means that the function grows at a rate that is at constant multiple of for large enough
values of n.
stands for oder of , indicating the growth rate in terms of the upper limit
represents the upper bound on the growth rate of . it helps to classify the complexity of the algorithm.
To say that is to assert that there exist a positive constant and a value
for instance give two functions that represent the time complexity of two different algorithms for processing a an array
of n elements:
1. we say that this function is because as n grows, the. dorminant term in the function is
The constant factors like 3 and + 2 don't affect the growth rate for large n
Example
1. Find upper bound for
To find the upper bound for the function using Big O . we aim to identify a function such that
does not grow faster than for some constant and for all n greater than some
for all
The term that grows the fastest as n increases is so we are considering a quadratic growth rate as the
Therefore
Exercise
There is no unique set of values for n0 and c in proving the asymptotic bounds. Let us consider,
Omega- Notation
Similar to the O discussion, this notation gives the tighter lower bound of the given algorithm
it is represented as
is a lower bound of
Theta Notation
The 𝛩-Notation basically decide whether the upper and the lower bound of a given algorithmic function are the same.
The average running time is between the lower and the upper bounds.
When the lower and upper bounds are the same then the 𝛩-Notation will also have the same growth rate.Similarly,
when the best and the worst cases are the same then the average case will also be the same.
Definition:
Example:
Bounding below:
Bounding above:
Therefore with constants , and which satisfies the defination of Big Theta:
1. Loops: The running time of a loop is, at most, the running time of the statements inside the
loop (including tests) multiplied by the number of iterations.
Total time =
Sorting
When you have a significant database, you might think of way to sort it
you need to arrange names in alphabetical order , students by gradem customers by zip code, cities in order of increasing
population, countries by GDP
sorting helps in structuring data in a way that makes it more accessible and easier to understand.
Sorting isn't just about organization for the sake of clarity; it also serves a functional purpose in the context of searching
through data.
Sorting data may also be a preliminary step to searching it. for exampe in binary search
Because sorting is so important and potentially so timeconsuming, it has been the subject of extensive research in
computer science.
simpler sorting algorithms include selection sort , bubble sort and insertion sort