Question

Compare the algorithm below: Algorithm 1 for i= 0 to i = 4 get the num[i]...

Compare the algorithm below:

Algorithm 1

for i= 0 to i = 4

get the num[i]

sum = sum + num[i]

Average = sum / 5

Print sum

Print average

Algorithm 2

  1. for i= 0 to i = 4
  2. get the num[i]
  3. for i= 0 to i = 4
  4. sum = sum + num[i]
  5. Average = sum / 5
  6. Print sum
  7. Print average

Calculate the complexity of the algorithm. Choose the most efficient algorithm, explain your choice.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
Lets take n = 4

Algorithm 1:
It is one loop running for 4 times.
So, Time complexity = O(n)


Algorithm 2:
first loop running for 4 times.
and second loop is also running for 4 times.
So, Time complexity = O(n*n) = O(n^2)

So, The algorithm which has less time complexity is the best algorithm.
So, Algorithm 1 is most efficient than algorithm 2.
Add a comment
Know the answer?
Add Answer to:
Compare the algorithm below: Algorithm 1 for i= 0 to i = 4 get the num[i]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • def sorted(numbers): num = numbers[0] for i in numbers: if i < num: return False num...

    def sorted(numbers): num = numbers[0] for i in numbers: if i < num: return False num = i return True State a best-case and a worst-case scenario for your algorithm. Explain the Big-O complexity of each scenario. Use comparison as the basic unit of computation. Take into account the complexity of any list operation you use

  • Consider the problem below and the algorithm that has been created to solve the problem. Find...

    Consider the problem below and the algorithm that has been created to solve the problem. Find and correct the errors in the algorithm. Re-write the algorithm below so that it correctly solves the problem. Briefly explain the error and the correction. Problem Create an algorithm that will calculate the average of a group of numbers that are input and print the average. Calculate_Average 1 Get num1, num2, num3 2 sum = num1 - num2 + num3 3 average = sum/4...

  • Hello, This is what i wrote: def squareEach(nums): answer = [] for num in nums: answer.append(num*num)...

    Hello, This is what i wrote: def squareEach(nums): answer = [] for num in nums: answer.append(num*num) return answer def sumList(nums): answer = 0 for num in nums: answer += num return answer def toNumbers(nums): new_nums = [] for i in nums: new_nums.append(int(i)) return new_nums def main(): fileName = input("What file are the numbers in? ") sum = 0 with open(fileName, 'r') as infile: for line in infile: nums.append(line) print(nums) nums = toNumbers(line) print(nums) squares = squareEach(nums) print(nums) sum += sumList(squares)...

  • I have this code for python: num = int(input()) if num == 0: print('No change') else:...

    I have this code for python: num = int(input()) if num == 0: print('No change') else: dol = num // 100 num %= 100 Quarters = num // 25 num %= 25 Dimes = num // 10 num %= 10 Nickels = num // 5 num %= 5 Pennies = num if dol > 0: if dol == 1: print('1 Dollar') if Dollars > 1: print(dol,'Dollars') if Quarters > 0: if Quarters == 1: print('1 Quarter') if Quarters > 1:...

  • 3. Calculate the time complexity of the following algorithm: 1. Initialize sum to 0. I 2....

    3. Calculate the time complexity of the following algorithm: 1. Initialize sum to 0. I 2. Input the value of n. 3. While i = 0 to n-1 do a. sum=sumti; ; b. Increment i

  • 3) [16 points totall Consider the following algorithm: int SillyCalc (int n) { int i; int Num, answer; if (n &lt...

    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*...

  • 3) [16 points total] Consider the following algorithm int SillyCalc (int n) int i; int Num, answer; if (n <=...

    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-Write an algorithm to get two numbers from the user (as inputs) and calculate the sum...

    1-Write an algorithm to get two numbers from the user (as inputs) and calculate the sum of two numbers and print out the result. (draw the flow chart) 2-Write an algorithm to get a number from the user and calculate the square of that number and print out the result.

  • Please, I need help debuuging my code. Sample output is below MINN = 1 MAXX =...

    Please, I need help debuuging my code. Sample output is below MINN = 1 MAXX = 100 #gets an integer def getValidInt(MINN, MAXX):     message = "Enter an integer between" + str(MINN) + "and" + str(MAXX)+ "(inclusive): "#message to ask the user     num = int(input(message))     while num < MINN or num > MAXX:         print("Invalid choice!")         num = int(input(message))     #return result     return num #counts dupes def twoInARow(numbers):     ans = "No duplicates next to each...

  • Can I please have a simple pseudocode algorithm of this problem. 1 Design an algorithm that...

    Can I please have a simple pseudocode algorithm of this problem. 1 Design an algorithm that will read a series of 50 integers at the terminal. The first integer is special, as it indicates how many more integers will follow. Your algorithm is to calculate and print the sum and average of the integers, excluding the first integer, and display these values to the screen.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT