
please also explain how the answer came about if possible
Let T(n) be the time taken for input n
Base Case is T(1) = 1
One while loop which runs for n time and One recursive call of
2*RecursiveCall(n/2)
T(n) = 2*T(n/2) + n
We Can solve the using Master's theorem :-
a = 2 , b = 2, f(n) = n
g(n) = nlogb a = nlog
22 = n
We can see f(n) = g(n) , Hence T(n) = O(nlogb
a log n) =>
T(n) = O(nlog n)
please also explain how the answer came about if possible b) Base 7. Master Theorem (3...
3. Recursive Program (6 points) Consider the following recursive function for n 1: Algorithm 1 int recurseFunc(int n) If n 0, return 1. If n 1, return 1 while i< n do while j <n do print("hi") j 1 end while i i 1 end while int a recurse Func(n/9); int b recurse Func (n/9) int c recurse Func (n/9) return a b c (1) Set up a runtime recurrence for the runtime T n) of this algorithm. (2) Solve...
6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2) will swap the values stored in the variables v1 and v2. Note that % is the modulus operation, and will return the integer remainder r of a/b, i.e., r-a%b Require: Array P with n > 0 values 1: i-1, j-n-l 2: while i<=j do for a=i to j by i do 4: 5: 6: 7: if Pla>Pat 11 and Pla]%2--0 then swap(Plal, Pla+1l) end...
3) [16 points totall Consider the following algorithm: int SillyCalc (int n) { int i; int Num, answer; if (n < 4) 10; return n+ else f SillyCalc(Ln/4) answer Num Num 10 for (i-2; i<=n-1; i++) Num + = answer + answer; answer return answer } Do a worst case analysis of this algorithm, counting additions only (but not loop counter additions) as the basic operation counted, and assuming that n is a power of 2, i.e. that n- 2*...
Explain please
II. (7 points) Consider the following bit of pseudocode: for (int k = 1; ks Ign; k++) (for (int i = 1; i r: i++) Print "Hello World"; for (int j-1;jsn:j++) Print "Hello World" when n = 2, how many times will "Hello World" be printed? When n 4, how many times will "Hello World be printed? O Assuming that the print is the basic operation, what is the complexity function of this pseudocode?
II. (7 points) Consider...
III. ASSIGNMENT 2.1 As discussed in class, the example program enumerates all possible strings (or if we interpret as numbers, numbers) of base-b and a given length, say l. The number of strings enumerated is b l . Now if we interpret the outputs as strings, or lists, rather than base-b numbers and decide that we only want to enumerate those strings that have unique members, the number of possible strings reduces from b l to b!. Furthermore, consider a...
1-Is it possible to run a program without a main() function? Yes No 2- How many main() functions can one have in a program? 2 This depends on what compiler you use. As many as you like 1 3- What does the following code fragment leave in x? char a = 'A'; int x = sizeof (a); 1 Depends on what compiler you use. 4 2 4- True or false: In a C program I can have two functions with...
T2(N) = Θ(N') Which algorithm is the least preferred one? a) Algorithm 1 b) Algorithm 2 ) Algorithm 3 Algorithm 1 and 3 2. Given the following function: (10 points) int F(int N) if (N0lIN1) return 2; else return N *FON-2); Show steps to find out F(5) Also give the runtime
1. Order following function by growth rate: N, √N, N1.5, N log (N), log (log (N)), log (N) log (N), N2, 2N, 200, NN 2. Give a useful Θ (big Theta) estimation for each of following function t(n). a. t(n) = 122 * 212 b. t(n) = 2log2(n2) + log4(n ) + (log2 n) 2 + (log2 (202)) 2 c. t(n) = 3t(n/2) + n d. t(n) = 3t(n/2) + (n+1)(n-1) e. t(n) = 4t(n/2) + (n2 + n-1) f....
Here is a recursive algorithm that answers the same question as posed on Group HW3, finding the number of people who are taller than everyone before them in line. NumCanSeeRec(a1,... , an : list of n 2 1 distinct heights) (a) ifn -1 then (b return 1 (c) c= ŅumCanSeeRee(a1, , an-1) d) for i:- 1 ton- 1 (e) if a, an then return c (g) return c+1 Answer the following questions about this algorithm. Please show your work. (a)...
3) [16 points total] Consider the following algorithm int SillyCalc (int n) int i; int Num, answer; if (n <= 4) return n 10; else { Num-SillyCalcl n/4) answer = Num + Num + 10; for (i-2; i<-n-1; ++) answer- answer+ answer; return answer; Do a worst case analysis of this algorithm, counting additions only (but not loop counter additions) as the basic operation counted, and assuming that n is a power of 2, i.e. that n- 2* for some...