Introduction to Algorithms
Introduction to Algorithms
Algorithms
An algorithm is “a finite set of precise instructions for
performing a computation or for solving a problem”
– A program is one type of algorithm
• All programs are algorithms
• Not all algorithms are programs!
– Directions to somebody’s house is an algorithm
– A recipe for cooking a cake is an algorithm
– The steps to compute the factorial of a given
number is an algorithm
Properties of an Algorithm
Algorithms generally share a set of properties:
Unambiguous − Algorithm should be clear and
unambiguous. Each of its steps (or phases), and their
inputs/outputs should be clear and must lead to only one
meaning.
Input − An algorithm should have 0 or more well-defined
inputs.
Output − An algorithm should have 1 or more well-defined
outputs, and should match the desired output.
Definiteness: the steps are defined precisely
Correctness: should produce the correct output
Finiteness: the steps required should be finite
Effectiveness: each step must be able to be performed in a
finite amount of time
Algorithm Complexity
Time complexity is a type of computational complexity that
describes the time required to execute an algorithm.
The time complexity of an algorithm is the amount of time it
takes for each statement to complete.
As a result, it is highly dependent on the size of the processed
data.
It also aids in defining an algorithm's effectiveness and
evaluating its performance.
Space Complexity
The amount of memory used by a program to execute it is
represented by its space complexity. Because a program
requires memory to store input data and temporal values while
running.
Asymptotic Notations
Asymptotic notations are the mathematical notations used to
describe the running time of an algorithm when the input tends
towards a particular value or a limiting value.
Big-O Notation (O-notation) :Big-O notation represents the upper
bound of the running time of an algorithm. Thus, it gives the worst-
case complexity of an algorithm.
Omega Notation (Ω-notation) :Omega notation represents the
lower bound of the running time of an algorithm. Thus, it provides
the best case complexity of an algorithm.
Theta Notation (Θ-notation) :Theta notation encloses the function
from above and below. Since it represents the upper and the lower
bound of the running time of an algorithm, it is used for analyzing
the average-case complexity of an algorithm.
Asymptotic Notations
Asymptotic notations are the mathematical notations used to
describe the running time of an algorithm when the input tends
towards a particular value or a limiting value.
Big-O Notation (O-notation) :Big-O notation represents the upper
bound of the running time of an algorithm.
Omega Notation (Ω-notation) :Omega notation represents the
lower bound of the running time of an algorithm.
Theta Notation (Θ-notation) :Theta notation encloses the function
from above and below. Since it represents the upper and the lower
bound of the running time of an algorithm,
O-notation
O(g(n)) = {f(n) :
positive constants c and n0, such that n n0,
(g(n)) = {f(n) :
positive constants c and n0, such that n
n0,
we have 0 cg(n) f(n)}
g(n) is an asymptotic LOWER bound for f(n).
-notation
x: -17 -5 3 6 142 21 12 45
x: -17 -5 3 6 142 21 12 45
x: -17 -5 3 6 12 21 14245
Selection Sort
/*Location of smallest element */
3 6 7 11 32 33 53
Binary Search
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33
int mid;
if(low<=high){
mid=(low+high)/2;
if(K==a[mid]){
return(1);
}
else if(k<a[mid]){
return binarySearch(a,n,K,low,mid-1);
}
else
return binarySearch(a,n,k,mid+1,high);
}
else return(0);
}