Data Structure A-Week2
Data Structure A-Week2
Algorithms
Week-2
Contents
i++;
i < n;
So the instructions are 2n ( n for loop, no. of iterations)
Find 4n???
Time Complexity
4n (no. of iterations)
Time Complexity
2
2
2n
4n
So 2+2+2n+4n = 4+6n
f(n) = 4 + 6n
Time Complexity-Exercise
int ArraySum()
{
a=[2,6,7]
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+a[i]
}
return sum;
}
Time Complexity-Exercise
int ArraySum()
{
a=[2,6,7]
int sum=0;
for(int i=0;i<n;i++)
{
sum=sum+a[i]
}
return sum;
}
Hint Find 5n+4???
Time Complexity
• Worst, Average and Best Cases
• Asymptotic Notations
• Common Time Complexities
Worst, Average and Best Cases
• Worst Case Analysis
In the worst case analysis, we calculate upper bound on running time
of an algorithm.
Find Upper Bound in 4 + 6n??
• Average Case Analysis
In average case analysis, we take all possible inputs and calculate
computing time for all of the inputs.
• Best Case Analysis
Asymptotic Notations
1. Θ Notation
The theta notation bounds a functions from above and below, so it
defines exact asymptotic behavior.
A simple way to get Theta notation of an expression is to drop low
order terms and ignore leading constants.
3n3 + 6n2 + 6000 = Θ(n3)
Asymptotic Notations
2. Ω Notation
• Ω notation provides an asymptotic lower bound.
• Ω Notation can be useful when we have lower bound on time complexity of an
algorithm
• The best case performance of an algorithm is generally not useful, the Omega
notation is the least used notation among all three.
• Time complexity of all computer algorithms can be written as Ω(1)
3. Big O Notation
The Big O notation defines an upper bound of an algorithm, it bounds a function only
from above.
• Linear , Quadratic and Constant Time
Big O Notation Usage
1. Constant Time
2. Linear Time
3. Quadratic time
Big O Notation Usage
1. Constant Time
• An algorithm is said to be constant time if the value of T(n) is bounded by a value
that does not depend on the size of the input.
int a[4]=[25,16,14,10]
• For example, accessing any single element in an array takes constant time as only
one operation has to be performed to locate it.
2. Linear Time
• An algorithm is said to take linear time if its time complexity is O(n)
• This means that the running time increases at most linearly with the size of the
input.
4 + 6n
Big O Notation Usage
3. Quadratic Time
An algorithm is said to be quadratic time if T(n) = o(n2).
6n2 + 4n+1
n Linear: run time varies directly with n. Typically, a small amount of processing is
done on each element.
n log n When n doubles, run time slightly more than doubles. Common in programs which
break a problem down into smaller sub-problems, solves them independently, then
combines solutions
n2 Quadratic: when n doubles, runtime increases fourfold. Practical only for small
problems; typically the program processes all pairs of input (e.g. in a double nested
loop).
n3 Cubic: when n doubles, runtime increases eightfold
2n Exponential: when n doubles, run time squares. This is often the result of a natural,
“brute force” solution.
Size does matter
Nlog2N 5N N log2N N2 2N
8 3 40 24 64 256
16 4 80 64 256 65536
32 5 160 160 1024 ~109
64 6 320 384 4096 ~1019
128 7 640 896 16384 ~1038
256 8 1280 2048 65536 ~1076
COMPLEXITY CLASSES
Time (steps)
27
27
Insertion Sort
Insertion Sort
• Insertion sort is a very simple method to sort numbers in an
ascending or descending order.
• It can be compared with the technique how cards are sorted at the
time of playing a game.
• The numbers, which are needed to be sorted, are known as keys.
Insertion Sort - Steps
Example
Insertion Sort- Algorithm
for j = 1 to A.length
key = A[j]
i=j–1
while i >= 0 and A[i] > key
A[i + 1] = A[i]
i = i -1
A[i + 1] = key
Dry Run??
Insertion Sort- Code
void insertionSort()
{
int arr[6]=[10,6,8,12,9,20]
int key, j;
for (int i = 1; i < arr.Length; i++)
{
key = arr[i];
j = i-1;
Input:
a[24,8,25,63,14,7]
Output:
a[63,25,24,14,8,7]