Asymptotic Analysis exercises in C Programming in C
Asymptotic Analysis 1: What is the running time of the following piece of code ?
for(inti=0;i<n*n;i++)Â Â Â sum=sum-i;...
Asymptotic Analysis 2: What is the running time of the following piece of code ?
i=1;while(i<=n){Â Â Â j=1;Â Â Â while(j<=n)Â Â Â Â Â j=j*2;Â Â Â i=i+1;}...
Asymptotic Analysis 3: What is the running time of the following piece of code ?
for(i=1;i<=n;i++){Â Â Â j=1;Â Â Â while(j<=n)Â Â Â Â Â j=j*2;}...
Asymptotic Analysis 4: What is the running time of the following piece of code ?
int sum=0,a=0,i=0;while(i<n){Â Â Â a=0;Â Â Â while(a<i)Â Â Â {Â Â Â Â Â Â sum++;Â Â Â Â Â Â a++;Â Â Â }Â Â Â i++;}...
Asymptotic Analysis 5: What is the running time of the following piece of code ?
int i=1, s=1; while(s<=n){Â Â Â i++;Â Â Â s+=i;}...
Asymptotic Analysis 6: What is the running time of the following piece of code ?
int i, count =0;for(i=1;i*i<=n;i++)Â Â Â count++;...
Asymptotic Analysis 7: What is the running time of the following piece of code ?
int i,j,k, count =0;for(i=n/2;i<=n;i++)Â Â Â for(j=1;j+n/2<=n;j=j++)Â Â Â Â Â Â for(k=1;k<=n;k=k*2)Â Â Â Â Â Â Â Â Â count++;...
Asymptotic Analysis 8: What is the running time of the following piece of code ?
int i,j,k, count =0;for(i=n/2;i<=n;i++)Â Â Â for(j=1;j<=n;j=2*j)Â Â Â Â Â Â for(k=1;k<=n;k=k*2)Â Â Â Â Â Â Â Â Â count++;...
Asymptotic Analysis 9: What is the running time of the following piece of code ?
void function(int n){Â Â Â if(n==1) return ;Â Â Â for(int i=1; i<=n; i++)Â Â Â Â Â for(int j=1; j<=n;j++)Â Â Â Â Â Â Â Â break;}Â ...
Asymptotic Analysis 10: What is the running time of the following piece of code ?
void function(int n){Â Â Â for(int i=1; i<=n; i++)Â Â Â Â Â for(int j=1; j<=n;j+=i)Â Â Â Â Â Â Â Â printf("1");}...
Asymptotic Analysis 11: What is the running time of the following piece of code ?
void function(int n){Â Â Â for(int i=1; i<=n; i++)Â Â Â Â Â for(int j=1; j<=n;j*=2)Â Â Â Â Â Â Â Â printf("1");}...
Asymptotic Analysis 12: What is the running time of the following piece of code ?
void function(int n){Â Â Â for(int i=1; i<=n/3; i++)Â Â Â Â Â for(int j=1; j<=n;j+=4)Â Â Â Â Â Â Â Â printf("1");}...
Asymptotic Analysis 13: What is the running time of the following piece of code ?
void function(int n){Â Â Â int i =1;Â Â Â while(i<n){Â Â Â Â Â Â int j=n;Â Â Â Â Â Â while(j>0)Â Â Â Â Â Â Â Â j/=2;Â Â Â Â Â Â i*=2;Â Â Â }}...
Asymptotic Analysis 14: What is the running time of the following piece of code ?
int power(int b, int n){Â Â Â int p=1, q=b, m=n; Â Â Â while(m>0) Â Â Â { Â Â Â Â Â Â if(m%2!=0) Â Â Â Â Â Â Â Â Â p*=q;Â Â Â Â Â Â m/=2; Â Â Â Â Â Â q*=q; Â Â ...
Asymptotic Analysis 15: What is the running time of the following piece of code ?
for(i=n; i>0; i/=2){Â Â Â for(j=1; j<n; j*=2)Â Â Â {Â Â Â Â Â Â for(k=0; k<n; k+=2)Â Â Â Â Â Â Â Â Â sum=sum+1;Â Â Â }}
...
Asymptotic Analysis 16: What is the running time of the following piece of code ?
for(k=1; k<=n; k*=2){Â Â Â for(i=0; i<k; i++)Â Â Â {Â Â Â Â Â Â for(j=0; j<n; j+=2)Â Â Â Â Â Â Â Â Â sum=sum+1;Â Â Â Â Â Â for(j=1; j<n; j*=2)Â Â Â Â Â Â Â ...
Asymptotic Analysis 17: What is the running time of the following piece of code ?
for(i=1; i<=n; i++){   res=x+y+x+res;    if((T[i]+k)<b)       for(j=1; j<=n; j++)          res=res+T[j];    else       res=res+T...
Asymptotic Analysis 18: What is the running time of the following program?
int C, N, R0, R1, I;
void A(int R){Â Â Â int I; Â Â Â for(I=1;I<=N;I++) Â Â Â Â Â Â R=R*I;}
void B(int R){ Â Â Â int I, J; Â Â Â J=1; Â Â Â for(I=1;I<=N;I++) Â Â Â { Â Â ...
Asymptotic Analysis 19: We can write a function that finds all the prime numbers less than \texttt{n} using the sieve of Eratosthenes:
Start  Put numbers from 2 to n in a sieve (array)   Repeat     Select and remove from the sieve the smallest integer  ...
Asymptotic Analysis 20: Without performing any calculations, give the worst case time complexity of the following function. Justify your answer.
void f(int k, int n){Â Â Â int i;Â Â Â for (i=0; i< n && k >0; i++)Â Â Â Â Â Â k = k / 2;}
...
Asymptotic Analysis 21: Without performing any calculations, give the worst case time complexity of the following function. Justify your answer.
void f(int n, int m, int L){Â Â Â int i=k=1, j=m;Â Â Â while((i<=n && j>=1) || k<=L)Â Â Â {Â Â Â Â Â Â...
Asymptotic Analysis 22: Give the worst case time complexity of the following two functions. Justify your answer.
void function1(int n){   int i, k, sum=0;   for(i=0; i<n; i++)      if(i<n/2)         sum=sum+1;      else Â...
Complexity of sum of square root: Calculate and justify the time complexity of the following function:int f(int n){Â Â Â int j = 10 ; Â Â Â while (j < n) {Â Â Â Â Â j += sqrt(j);Â Â Â }Â Â Â return j;}...
Dealing with Logs: Given that \( \log_{10}4 = 0.6020 \) and \( \log_{10}5 = 0.6989 \) , find the value of \( \log_{20}10 \)....
Series Sum: Compute the sum of the following series: \( \sum_{i=1}^n i \times 2^i\)....
Series Sum 2: Compute the sum of the following series: \( \sum_{i=1}^n i^2 \times 2^i\)....
Series Sum 3: Which of the following two terms is larger: \( \sum_{i=1}^n i^2 \) or \( \sum_{i=1}^{n^2} i \)....
Asymptotic Analysis 23: Give the worst case time complexity of the following function. Justify your answer.
i = 1;while (i < n) {Â Â j = 2;Â Â while (j < n) Â Â Â Â j = j * j;Â Â i++;}
...
Asymptotic Analysis 24: How do these pairs of functions compare asymptotically:
$$n^{17}$$ and $$2^n$$
$$2^{n^2}$$ and $$10^n$$
...