Question

In: Computer Science

Determine the running time of each of the algorithms below. For questions 2a. and 2b., you...

Determine the running time of each of the algorithms below. For questions 2a. and 2b., you may determine the running time line-by-line (see Lecture 2, slide No. 8). For questions 2c – 2f, you may determine the running time by focusing on the basic operation of the algorithm.

a,sum = 0; for (i=1; i<=n; i++) sum += n;

b. Algorithm maxVal (numbers, n) currentVal ← numbers [0] for i ← 1 to n − 1 do if numbers [i] > currentVal then currentVal ←A[i] { increment counter i } return currentVal

c. Algorithm prefix1(X, n) //Input array X of n integers //Output array A of prefix averages of X A ← new array of n integers for i ← 0 to n − 1 do s ←X[0] for j ← 1 to i do s ←s + X[j] A[i] ←s / (i + 1) return A

d. Algorithm prefix2(X, n) //Input array X of n integers //Output array A of prefix averages of X A ← new array of n integers s ← 0 for i ← 0 to n − 1 do s ←s + X[i] A[i] ←s / (i + 1) return A

e. Algorithm SumTripleArray(X, n) //Input triple array X[][][] of n by n by n integers //Output sum of elements of X s ← 0 for i ← 0 to n − 1 do for j ← 0 to n − 1 do for k ← 0 to n − 1 do s ←s + X[i][j][k] return s

f. sum = 0; for( i = 0; i < n; i++) for( j = 0; j < n * n; j++) sum++;

Solutions

Expert Solution

a.

sum = 0;

for (i=1; i<=n; i++)

sum += n

the for loop runs for n time doing a constant operation each time so total running time is O(n)

b.

maxVal (numbers, n)

currentVal ← numbers [0]

for i ← 1 to n − 1

do if numbers [i] > currentVal

then currentVal ←A[i]

{ increment counter i }

return currentVal

the for loop runs for n-1 times doing at max 3 operation each time so total operation is <3n and runnning time =O(n)

c.

prefix1(X, n) //Input array X of n integers //Output array A of prefix averages of X

A ← new array of n integers

for i ← 0 to n − 1

do s ←X[0]

for j ← 1 to i do

s ←s + X[j]

A[i] ←s / (i + 1)

return A

The outer i loop runs for n times. and for each i value inner j loop runs for i times doing constant operations each time

so number of operations is c(1+2+3.......n-1) =O(n^2)

d.

prefix2(X, n) //Input array X of n integers //Output array A of prefix averages of X

A ← new array of n integers

s ← 0

for i ← 0 to n − 1 do

s←s + X[i]

A[i] ←s / (i + 1)

return A

there is only 1 i loop running n times, inside which a constant number of operations are being dome so running time is O(n)

e.

SumTripleArray(X, n) //Input triple array X[][][] of n by n by n integers //Output sum of elements of X

s ← 0

for i ← 0 to n − 1 do

for j ← 0 to n − 1 do

for k ← 0 to n − 1 do

s ←s + X[i][j][k] return

there are 3 nested loops i,j,k each running n times thus total running time is O(n^3)

f.

sum = 0;

for( i = 0; i < n; i++)

for( j = 0; j < n * n; j++)

sum++;

the outer i loop runs for n times and for each i value , the inner loop runs for n^2 times. so total running time is O(n^3)


Related Solutions

Suppose you have algorithms with the five running times listed below. (Assume these are the exact...
Suppose you have algorithms with the five running times listed below. (Assume these are the exact running times.) How much slower (i.e., you need check the ratio) do each of these algorithms get when you (a) double the input size, (b) increase the input size by one? 5nlog(n)
Provide and implement three completely different algorithms of different running time that will check if two...
Provide and implement three completely different algorithms of different running time that will check if two strings are anagrams.
For the reaction: 3A + 2B -----> Products Use the data below to determine the rate...
For the reaction: 3A + 2B -----> Products Use the data below to determine the rate law [A] (M) [B] (M) Rate (Ms-1) 0.300 0.200 3.067 x 10-3 0.600 0.400 8.680 x 10-3 0.900 0.200 5.131 x 10-3
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In...
WRITE USING C++ CODE, TOPIC :Analysis of Algorithms Computer Science. PLEASE DONT FORGET RUNNING TIME In this lab, you will implement Heap Sort algorithm for the same inputs. For each algorithm, and for each n = 100, 200, 300, 400, 500, 1000, 4000, 10000, measure its running time and number of steps when the input is (1) already sort, i.e. n, n-1, …, 3, 2,1; (2) reversely sorted 1, 2, 3, … n; (3) random permutation of 1, 2, …,...
Just need 2a and 2b answered. Already have number one. Just included in case you needed...
Just need 2a and 2b answered. Already have number one. Just included in case you needed it for part two. 1. On January 1, 2020, Hawkeye Air leased a new airplane for a term of 8 years. The expected life of the airplane is 20 years. There are no rights to purchase the asset at the end of the term, no bargain purchase option, and no residual value guarantee. The lease stipulates that Hawkeye Air makes annual payments of $550,000...
Consider real time process for the following 3 process with the period and running time below:...
Consider real time process for the following 3 process with the period and running time below: Process Period Running time 1 80 40 2 60 25 3 39 15 Show that Rate Monotonic Scheduling will fail to schedule the 3 processes to satisfy their requirements. b. Suppose you can change the period that process 1 will take to run. What is the minimum value such that Rate Monotonic Scheduling will work? Explain your answer
Provide a reason for each process state transition below: a) Ready -> Running: b) Running ->...
Provide a reason for each process state transition below: a) Ready -> Running: b) Running -> Ready: c) Running -> Blocked: d) Blocked -> Ready:
In questions below determine whether each of the following sets is countable or uncountable. For those...
In questions below determine whether each of the following sets is countable or uncountable. For those that are countably infinite exhibit a one-to-one correspondence between the set of positive integers and that set. 1) The set of positive rational numbers that can be written with denominators less than 3. 2) The set of irrational numbers between sqrt(2) and π/2.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT