1.
The 2 for loops in the code segment are not independent and so the product rule cannot be used to compute the time complexity.
Since both i loop and j loop are dependent upon each other, let's analyze how both the loops work using the following table :
| Value of i | n | n/2 | n/4 | 1 | |||||
| j loop runs for | log n times | log ( n / 2 ) times | log ( n / 4 ) times | log ( 1 ) times |
Let the running time of the algorithm be represented as T(n).
So, the time complexity is calculated as :
T(n) = Sum of the total number of times j loop runs
= log n + log ( n / 2 ) + log ( n / 4 ) + .................... log ( 1 )
= log ( n * n /2 * n / 4 .......... 1 ) + 0 [ logarithmic property ]
<= log ( n^n)
<= n log n
So,
T(n) = Θ(n log n)
Hence, the running time of the given algorithm is Θ(n log n).
1. Analyze the running time of the following algorithm and write it using ( notation. You...
In Big-Θ notation, analyze the running time of the following pieces of code/pseudo-code. Describe the running time as a function of the input size (here, n) int *a = new int [10]; // new is O(1) int size = 10; for (int i = 0; i < n; i ++) { if (i == size) { int newsize = 3*size/2; int *b = new int [newsize]; // new is O(1) for (int j = 0; j < size; j ++)...
Analyze the running time of the following algorithms asymptotically. (a) Algorithm for-loop(n): P = 1 for i = 1 to 5n^2 do p = p times i return p (b) Algorithm for-loop(n): s = 0 for i = 1 to n do for j = I to n do s = s + i return s (c) Algorithm WhileLoop(n): x = 0; j = 2; while (j = n){x = x+ 1; j =j times 2;}
In Big-Θ notation, analyze the running time of the following pieces of code/pseudo-code. Describe the running time as a function of the input size (here, n) for(int i=n-1; i >=0; i--){ for(int k=0; k < i*n; k++){ // do something that takes O(1) time } }
Describe the worst case running time of the following
pseudocode functions in Big-Oh notation in terms of the variable n.
Show your work
b) void func(int n) { for (int i = 0; i < n; i = i + 10) { for (int j = 0; j < i; ++i) { System.out.println("i = " + i); System.out.println("j = " + j);
1. Give the running time of the following procedures, using -notation. Show your work. procedure P. (n) S=0; for i n to 2n do for j = 5i to 5i+12 do ses+i-j. procedure ps(n) 560; for i« 5n to 6n do for 15 to i do for k j to i do ses+i-j. procedure pe(n) S=0; for i1 to 5n do begin j4i ; while j<i' do begin 5 5 +i-j; je 5; end end
Below is an algorithm using C++ language Question : Analyze the algorithm and provide in terms of exact number of steps A(n) or the algorithm. A(n) = Given your analysis provide an upper bound in terms of Big-O for Algorithm . O(n) = #include <iostream> #include <cstdlib> #include <cmath> using namespace std; int random_value(int range, long seed) { srand(seed); float generator = (float) rand() / (float) RAND_MAX; float random = floor(range * generator); return random; }...
Big-O notation. Consider the following function. int func1(int n) { int sum = 0, i; for(i = 0; i<n; i++;) { sum += i; return sum; } Express the running time of func1 as a function of n using big-O notation. Write a function that has the same functionality as func1, but runs in O(1) time.
A) Write the pseudocode for an algorithm using dynamic programming to solve the activity-selection problem based on this recurrence: c[i, j] = 0 if Si; = Ø max {c[i, k] + c[k,j] + 1} if Sij +0 ak eSij B) Analyze the running time (the time complexity) of your algorithm and compare it to the iterative greedy algorithm.
Analyze the time complexity of the following algorithm. You may assume that the floor function in line 2 takes Theta (1) time. Please show your work. Input: data: array of integers Input: n: size of data Output: median of data 1 Algorithm: MedianSelect 2 lim = [n/2] + 1 3 min = - infinity 4 for i = 1 to lim do 5 prev = min 6 min = infinity 7 for j = 1 to n do 8 if...
In this lab we are going to complete a profile of two sorting algorithms by running some tests to collect empirical data. 1. First we need to be able to generate some random integers. You can do this by including the following library : #include Now first run the following to generate a seed : srand (time(NULL)) You can then generate a random number using the function rand() 2. We will use two sort algorithms - Selection Sort and Bubble...