. Big O Notation.Thanks to Reges, Building Java Programs, 2nd edition.
Estimate the big-O complexity for each of these algorithms, and justify your answer.
To confirm your calculations, answers are provided at the end of the rubric. Your justification can be mathematical or written, formal or informal.
Rubric:
Correct Big-O classification of four problems
Justification of four problems
Big-O categories: 3.1. O(log n). 3.2. O(n). 3.3. O(n2).
3.4. O(1)
|
Problem |
Code fragment |
|
3.1 |
int sum = 0; int j = 1; while (j <= n) { sum++; j *= 2; } |
|
Big-O Category |
|
|
Justification (why did you pick the way you did?) |
|
|
3.2 |
int sum = 0; for (int j = 1; j < n; j++) { sum++; if (j % 2 == 0) { sum++; } } |
|
Big-O Category |
|
|
Justification (why did you pick the way you did?) |
|
|
3.3 |
int sum = 0; for (int i = 1; i <= n * 2; i++) { for (int j = 1; j <= n; j++) { sum++; } } |
|
Big-O Category |
|
|
Justification (why did you pick the way you did?) |
|
|
3.4 |
for (int j = 1; j < 100; j++) { sum++; sum++; } |
|
Big-O Category |
|
|
Justification (why did you pick the way you did?) |
3.1) The complexity of the algorithm is O(log(n))
All the single step execution path are considered as execution time of O(1), so
int sum = 0;------->O(1)
int j = 1;--------------> O(1)
Then while loop is executed, the value of j is the series, 1, 2,2^2,2^3,.....,2^k
If we put k equals to Log2n, we get 2Log2n which is n.
ie the time complexity of a code isO( log(n)) when loop variable is multiplied or divided by a constant value
------------------------------------------------------------------------------------------------------------------------------
3.2) Here the for loop executes n times and so the code inside the for loop.
So the final time complexity will be O(n) + O(1) = O(n)
Note here O(1) is the time complexity of code outside for loop
--------------------------------------------------------------------------------------------------------------------------------
3.3)Time complexity of code outside nested for loops= O(1)
Here we have nested for loop, so to find the time complexity , find the number of time inner for loop is executed.
Inner for loop is executed 2*n times
and complexity of inner for loop =O(n) as it executes codes inside it n times.
So total time complexity= O(1)+O(2n*n)=O(2n^2)= O(n^2), we can eliminate constant 2
----------------------------------------------------------------------------------------------------------------------------------
3.4)
In this case the loop variable i executes 100 times and so the code inside the for loop
So the time complexity of algo =O(100)= O(1), because complexity of any algorithm executing a constant number of times i can be considered as O(1)
. Big O Notation.Thanks to Reges, Building Java Programs, 2nd edition. Estimate the big-O complexity for...
Show the Big O Complexity of the following functions and loop constructions: (Please show work and explain) a. f(n) = 2n + (blog(n+1)) b. f(n) = n * (log(n-1))/2 c. int sum = 0; for (int i=0; i<n; i++) sum++; for (int j=n; j>0; j /= 2) sum++; d. int sum = 0; for (int i=n; i>0; i--) for (int j=i; j<n; j *= 2) sum++;
Analyze the following programs and show their time complexity functions and big-O notations. for(int i = 1; i <= n; i+=3) { for(int j=1; j <= n; j++) { if (j % 3 == 0) { // 4 assignments } if (2*i + 3 == 5) { // 17 assignments } } }
Please DONOT attempt this Big O question if you don't know the exact answer. Algorithms question (Big O): Please explain me in details the order of growth (as a function of N) of the running times of each of the following code fragments: a) int sum = 0; for (int n = N; n > 0; n /= 2) for(int i = 0; i < n; i++) sum++; b) int sum = 0; for (int i =...
Using C++ please explain
What is the Big-O time complexity of the following code: for (int i=0; i<N; i+=2) { ... constant time operations... Select one: o a. O(n^2) O b. O(log n) c. O(n) O d. 0(1) What is the Big-O time complexity of the following code: for(int i=1; i<N; i*=2) { ... constant time operations... Select one: O O a. O(n^2) b. 0(1) c. O(n) d. O(log n) O What is the Big-O time complexity of the following...
**C++ Question** For all of the following, determine the total operation count and then the Big-O of the given code segments: a. for (int j = 0; j < n; j++) for (int k = 0; k < j; k++) sum++; b. for (int i = 0; i < q*q; i++) for (int j = 0; j < i; j++) sum++; For all of the following, just determine the Big-O of the given code segments: c. for (int i =...
Which big-O expression best characterizes the worst case time complexity of the following code? public static int foo(int N) ( int count = 0; int i1; while (i <N) C for (int j = 1; j < N; j=j+2) { count++ i=i+2; return count; A. O(log log N) B. O(log N2) C. O(N log N) D. O(N2)
(10') 6. For each of the following code blocks, write the best (tightest) big-o time complexity i) for (int i = 0; ǐ < n/2; i++) for (int j -0: ni j++) count++ i) for (int í = 0; i < n; i++) for (int ni j0 - for (int k j k ni kt+) count++ İİİ) for (int í ー 0; i < n; i++) for(int j = n; j > 0; j--) for (int k = 0; k...
Show your work Count the number of operations and the big-O time complexity in the worst-case and best-case for the following code int small for ( i n t i = 0 ; i < n ; i ++) { i f ( a [ i ] < a [ 0 ] ) { small = a [ i ] ; } } Show Work Calculate the Big-O time complexity for the following code and explain your answer by showing...
For the following parts, try to get the best Big-O estimate that you can and briefly justify your answers. Part a) int i, j; int n = 100; for (i = 1; i <= n; i++) { for (j = 3*i; j <= n; j++) { printf("programming is fun\n"); } } Part b) int i, j; int n = 1000000; for (i = 1; i <= n; i++) { for (j = 1; j <= 10000; j++) { printf("%d %d\n",...