Complexity Analysis
int main() {
int a, b, i,sum=0;
a=10; 1 b=20; 1
sum=a+b; 1 O(1) printf("%d",sum);
}
• int main() {
int n, b, i,sum=0;
scanf("%d",&n);
sum=n*(n+1)/2 0(1) printf("%d",sum);
}
int main() {
int n, b, i,sum=0;
scanf("%d",&n);
for(i=0;i<n;i++){ 1,2,3,……n O(n) sum=sum+i;
}
printf("%d",sum);
}
int main() {
int n, b, i,count=0;
scanf("%d",&n);
for(i=0; i<n; i++) 1+2+3….n= n(n+1)/2=n^2+n/2 =O(n^2) {
for(j=0; j<n; j++) {
count=count+1;
} }
printf("%d",count);
}
int main() {
for(i=0; i<n; i++) {
for(j=0; j<n; j++) {
for(k=0;k<n;k++) {
count=count+1;
} } }
int main() {
for(i=0; i<n; i++) {
for(j=0; j<n; j++) n^2 n^2+n=O(n^2) {
count=count+1;
} }
for(i=0; i<n; i++) {
count=count+1; n
}
a() {
s=1;i=1;
While(s<=i) s=1 3 i=1 2 {
i++;
s=s+i;
Printf(“%d”,s);
} }
• int main()
• {
• int n, b, i,count=0;
• scanf("%d",&n);
• for(i=0; i^2<n; i++) n!
• {
• printf("something") ;
•
• } }
Big-O notation represents the upper bound of the running time of an algorithm. Thus, it gives the worst case complexity of an algorithm.
Say
f(n) is your algorithm runtime,
g(n) is an arbitrary time complexity f(n) = C(g(n)),
For some real constants c (c > 0) and n0 >1, f(n) <= c g(n) for every input size n (n > n0).
F(n)=O(g(n))
• Example
F(n)=3n+2 g(n)=n F(n)<=Cg(n)
3n+2<=Cn C=4 3n+2<=4n
n>=2n
F(n)=O(g(n))