Professional Documents
Culture Documents
Courses Login
Suggest an Article
Analysis of Algorithms | Set 5 (Practice Problems)
We have discussed Asymptotic Analysis, Worst, Average and Best Cases , Asymptotic Notations
and Analysis of loops in previous posts.
{ 3T(n-1), if n>0,
T(n) = { 1, otherwise
Solution:
Solution:
function(int n)
{
if (n==1)
return;
for (int i=1; i<=n; i++)
{
for (int j=1; j<=n; j++)
{
printf("*");
break;
}
}
}
▲
Time Complexity of the above function O(n). Even though the inner loop is bounded by n, but due to
break statement it is executing only once.
void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
for (int j=1; j<=n; j = 2 * j)
for (int k=1; k<=n; k = k * 2)
count++;
}
void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
▲
Time Complexity of the above function O(n log2n).
void function(int n)
{
int count = 0;
for (int i=n/2; i<=n; i++)
for (int j=1; j+n/2<=n; j = j++)
for (int k=1; k<=n; k = k * 2)
count++;
}
void function(int n)
{
int count = 0;
void function(int n)
{
int i = 1, s =1;
while (s <= n)
{
i++; ▲
s += i;
printf("*");
}
}
Solution: We can de ne the terms ‘s’ according to relation si = si-1 + i. The value of ‘i’ increases by
one for each iteration. The value contained in ‘s’ at the ith iteration is the sum of the rst ‘i’ positive
integers. If k is total number of iterations taken by the program, then while loop terminates if: 1 + 2 +
3 ….+ k = [k(k+1)/2] > n So k = O(√n).
void function(int n)
{
int count = 0;
for (int i=0; i<n; i++)
for (int j=i; j< i*i; j++)
if (j%i == 0)
{
for (int k=0; k<j; k++)
printf("*");
}
}
void function(int n)
{
int count = 0;
// executes n times
for (int i=0; i<n; i++)
This article is contributed by Mr. Somesh Awasthi. If you like GeeksforGeeks and would like to ▲
contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to
contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and
help other Geeks.
Please write comments if you nd anything incorrect, or you want to share more information about
the topic discussed above.
Recommended Posts:
Analysis of Algorithms | Set 1 (Asymptotic Analysis)
Analysis of Algorithms | Set 2 (Worst, Average and Best Cases)
Analysis of Algorithms | Set 3 (Asymptotic Notations)
Analysis of Algorithm | Set 4 (Solving Recurrences)
Analysis of Algorithms | Set 4 (Analysis of Loops)
Analysis of Algorithm | Set 5 (Amortized Analysis Introduction)
Algorithm Practice Question for Beginners | Set 1
Pseudo-polynomial Algorithms
Asymptotic Analysis and comparison of sorting algorithms
Analysis of algorithms | little o and little omega notations
Practice Questions on Time Complexity Analysis
Practice Set for Recurrence Relations
Analysis of different sorting techniques
Loop Invariant Condition with Examples of Sorting Algorithms
Analysis of Algorithms | Big-O analysis
▲
9
To-do Done 3.1
Based on 13 vote(s)
Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
COMPANY LEARN
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Video Tutorials
PRACTICE CONTRIBUTE
Company-wise Write an Article
Topic-wise Write Interview Experience
Contests Internships
Subjective Questions Videos