Count the number of assignments and comparisons in this algorithms.
Please follow the below answers with an explanation.
I have also provided the program code in python to demonstrate the concept
PYTHON CODE:
def loop1(n):
p=1
assignments=1
comparisions=0
for i in range(1,n*n+1):
# assignment
assignments+=1
p=p*i
comparisions+=1
print("comparisions=",comparisions," assignments=",assignments)
def loop2(n):
s=0
assignments=1
comparisions=0
for i in range(1,n*n+1):
for j in range(1,i+1):
# assignment
assignments+=1
s=s+i
comparisions+=i
print("comparisions=",comparisions,"
assignments=",assignments)
print('Loop1 N=5 ')
loop1(5)
print('Loop1 N=10 ')
loop1(10)
print('Loop2 N=5 ')
loop2(5)
print('Loop2 N=10 ')
loop2(10)
=========
OUTPUT:
CODE
Count the number of assignments and comparisons in this algorithms. a) Algorithm Loop1(n): p+ 1 for...
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;}
PART B- The Efficiency of Algorithms, 10 points 1. Show how you count the number of operations (not only . Consider the following two loops, 4 points: basic operations) required by the algorithm, 4 points: //Loop λ sumList (aList, n) ( thǐssum = 0; for (j = 1; j <= 10000; j++) sum - sum + j; lastSum = 5000; for (int i = 0; i <= n; i++) { // Loop B for (j = 1; j <= n;...
1. Consider the following well-known sorting algorithm, which is studied later in the book, with a counter inserted to count the number of key comparisons. ALGORITHM SortAnalysis(A[0..n − 1]) //Input: An array A[0..n − 1] of n orderable elements //Output: The total number of key comparisons made count ←0 for i ←1 to n − 1 do v ←A[i] j ←i − 1 while j ≥ 0 and A[j ]> v do count ←count + 1 A[j + 1]←A[j ]...
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...
(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...
Let A[1..n] be an array with n elements. Consider the Count-Occurrence algorithm with the pseudocode below. Count-Occurrence(A, n, k). count 0 for j = 1 to n if A[j] ==k count = count+1 print count Which of the following is the correct loop invariant for the for loop? At the start of each iteration jof the for loop, count represents the number of times that k occurs in the subarray A[1.j]. At the start of each iteration of the for...
Prime Number Programing in C Note: The program is to be written using the bitwise operation. Use an integer array (not a Boolean array) and a bit length (for instance 32 bits). In this program you will write a C program to find all prime numbers less than 10,000. You should use 10,000 bits to correspond to the 10,000 integers under test. You should initially turn all bits on (off) and when a number is found that is not prime,...
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 ( <=...
Show that the time complexity for the number of assignments of records for the Mergesort algorithm (Algorithms 2.2 and 2.4) is approximated by T (n) = 2nlgn. by Mingfu LI, CGUEE Algorithms Algorithms 2.2 : Mergesort O Problem Sort n keys in nondecreasing sequence. Inputs positive integer n, array of keys S index from 1 to n Output the array 5 containing the keys in nondecreasing sequence. void mergesort (int n, keytype S[]) (1 <u)и } keytype UI..h), ИI.m); copy...
Solve ques no. 2 a, b, c, d . Algorithm 1 Sort a list al,..., an for i=1 to n-1 do for j=1 to n-i do if aj > aj+1 then interchange a; and a;+1 end if end for end for (b) Algorithm 1 describes a sorting algorithm called bubble sort for a list al,...,an of at least two numbers. Prove that the algorithm is complete, correct and terminates. (2) Complexity of Algorithms (Learning Target C2) (a) What is the...