4)
a)
worst case complexity:O(n^2)
explanation:
outer loop runs for n-1 times
inner loop runs for i+3 for each iteration of i
let i=1, inner loop runs 1+3 times
let i=2, inner loop runs 2+3 times
,
,
,
let i=n-1, inner loop runs n-1+3 times
total : 1+3 + 2+3 + 3+3 + ,,,,,, + n-1 +3 = (1+2+3+,,+n-1)+3*(n-1)=
n*(n-1)/2 + 3*(n-1) = O(n^2) times
so multiplication will be performed :O(n^2)
b)
worst case complexity :O(nlogn)
outer loop runs n-2 times
inner loop runs logn times for each iteration of outer loop
total complexity : (n-2) *logn = O(nlogn)
so multiplication will be performed :O(nlogn)
c)
worst case complexity :O(n^3)
outer loop runs n times
inner middle loop runs n times
inner most loop runs n*2i times for each i of outer loop
for i=1 inner most loop runs n*2*1 times
for i=2 inner most loop runs n*2*2 times
for i=3 inner most loop runs n*2*3 times
,
,
for i=n inner most loop runs n*2*n times
total complexity : n*2*1 +n*2*2 +,,, + n*2*n = n*2*(1+2+3+,,,+n) =
n*2*(n*(n+1))/2 = O(n^3)
4. [16 marks total (6 marks each)] Do a worst-case analysis for the following algorithm segments, counting the number of multiplications which occur. I have marked the lines with the multiplications...
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*...
Find the worst case runtime f(n) for the following algorithms. Specify the number of operations executed for an input size n, for the worst case run time as a function of n. Circle statement(s) and draw a line to the right side specifying the number of operations. If statement(s) are a part of an iteration of n, specify the total number of iterations as a function of n. Algorithm-01 int sum = 0; int j = 1; while ( <=...
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...
(1) Give a formula for SUM{i} [i changes from i=a to i=n], where a is an integer between 1 and n. (2) Suppose Algorithm-1 does f(n) = n**2 + 4n steps in the worst case, and Algorithm-2 does g(n) = 29n + 3 steps in the worst case, for inputs of size n. For what input sizes is Algorithm-1 faster than Algorithm-2 (in the worst case)? (3) Prove or disprove: SUM{i**2} [where i changes from i=1 to i=n] ϵ tetha(n**2)....
Question 4 CLO3 The following Python script implements an algorithm to find and prints the max value in a list of values. MAX 0 def MaxVal (Ist): for i in Ist: if( MAX < i): MAX = i return (MAX) Marks (20,24,26,19,5,31,24,32,32,45 print (MaxVal (Marks) a. Express the number of operations in terms of a function f(n), where n is the input size. (1 Mark) b. What is the total number of operations in the for loop of the algorithm...
Searching/sorting tasks and efficiency analysis - Big-oh For each problem given below, do the following: 1. Create an algorithm in pseudocode to solve the problem. 2. Identify the factors that would influence the running time of your algorithm. For example, if your algorithm is to search an array the factor that influences the running time is the array size. Assign names (such as n) to each factor. 3. Count the operations performed by the algorithm. Express the count as a...
Refer to the following program sample to answer the parts (a-i) below. Keep in mind that low and high are both indices of array A. /1 Assume that A is a sorted array containing N integers // Assume that x is a variable of type int int low= 0; // Line 1 int high- N; // Line 2 while (low- high) I/ Line 3 m- (lowthigh)/2; // Line 4 (This is integer division) if (A [m]<) I/Line 5 then low-...
13. (i) For each of the following equations, find all the natural numbers n that satisfy it (a) φ(n)-4 (b) o(n) 6 (c) ф(n) 8 (d) φ(n) = 10 (ii) Prove or disprove: (a) For every natural number k, there are only finitely many natural num- bers n such that ф(n)-k (b) For every integer n > 2, there are at least two distinction integers that are invertible modulo n (c) For every integers a, b,n with n > 1...
Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler { /** * The number of consecutive shuffle steps to be performed in each call * to each sorting...
I'm not getting out put what should I do I have 3 text files which is in same folder with main program. I have attached program with it too. please help me with this. ------------------------------------------------------------------------------------ This program will read a group of positive numbers from three files ( not all necessarily the same size), and then calculate the average and median values for each file. Each file should have at least 10 scores. The program will then print all the...