1.
sumList(aList, n) {
thisSum = 0; //runs O(1) time
lastSum = 5000; //runs O(1) time
for(int i=0; i<=n; i++) { //runs O(n+1) times
thisSum = thisSum + aList[i] * 2; //runs O(n+1) times
}
return thisSum >= lastSum; //runs O(1) time
}
In total => O(1 + 1 + (n+1) + (n+1) + 1) time
Remove the constant times, which gives the overall time complexity of O(n) time complexity.
2.
Loop A runs n*10000 times. If n = 100, then it will execute 100*10000 times, which in total 1000000 times.
On the other hand, Loop B runs n*n times. If n = 100, then it will execute 100*100 times, which in total 10000 times.
As you can see, Loop B is faster than Loop A if and only if, n is less than 10000. If n is greater than 10000 then loop A runs faster.
PART B- The Efficiency of Algorithms, 10 points 1. Show how you count the number of...
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 ( <=...
Java code efficiency: Look at the implementation of the method called intervalSums below. Design and implement a more efficient runtime algorithm for intervalSums. public static long intervalSums(intll A, intl0 B) long count = 0; for (int i = 0; i < A.|ength; i++) { for (int j = i; j < A.length; j++) { int sum = 0; for (int k = i; k <= j; k++) { sum += A[k]; count += 1; B[i][j] = sum; if (j >...
Count the number of assignments and comparisons in this algorithms. a) Algorithm Loop1(n): p+ 1 for i = 1 to n do ppi b) Algorithm Loop2(n): SO for i = 1 to n do for j = 1 to i do Sesti
1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci number. By definition Fibonnaci(0) is 1, Fibonnaci(1) is 1, Fibonnaci(2) is 2, Fibonnaci(3) is 3, Fibonnaci(4) is 5, and so on. Your function may only use a constant amount of memory (i.e. no auxiliary array). Argue that the running time of the function is Θ(n), i.e. the function is linear in n. 2. (10 points) Order the following functions by growth rate: N, \N,...
Please DONOT attempt this Big O question if you don't know the exact answer. Algorithms question (Big O): Please explain me in details the order of growth (as a function of N) of the running times of each of the following code fragments: a) int sum = 0; for (int n = N; n > 0; n /= 2) for(int i = 0; i < n; i++) sum++; b) int sum = 0; for (int i =...
Practical 5: Write a program that implements several sorting algorithms, and use it to demonstrate the comparative performance of the algorithms for a variety of data sets. Need Help With this Sorting Algorithm task for C++ Base Code for sorting.cpp is given. The header file is not included in this. Help would be much appreciated as I have not started on this due to personal reasons #include <cstdlib> #include <iostream> #include <getopt.h> using namespace std; long compares; // for counting...
(10 pts.) Count the worst-case number of array element comparisons (A[j] < A[j-1]) made by InsertionSort on arrays of size n: void InsertionSort(int A[], int n) { for (int i = 1; i < n; ++i) for (int j = i; j > 0 && A[j] < A[j-1]; --j) swap(A[j], A[j-1]); } Do the same for the number of swap's. 2. Which function grows faster: 2^((lg?))2 or ?^(2019)? Justify your answer. 3. Use "name and conquer" to give a derivation...
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;}
How can I write this code (posted below) using vectors instead of arrays? This is the task I have and the code below is for Task 1.3: Generate twenty random permutations of the number 0, 1, 2, ..., 9 using of the algorithms you designed for Task 1.3. Store these permutations into a vector and print them to the screen with the additional information of unchanged positions and number of calls torand(). Calculate the total numbers of unchanged positions. Compare...
Q4) [5 points] Consider the following two algorithms: ALGORITHM 1 Bin Rec(n) //Input: A positive decimal integer n llOutput: The number of binary digits in "'s binary representation if n1 return 1 else return BinRec(ln/2)) +1 ALGORITHM 2 Binary(n) tive decimal integer nt io 's binary representation //Output: The number of binary digits in i's binary representation count ←1 while n >1 do count ← count + 1 return count a. Analyze the two algorithms and find the efficiency for...