The algorithm will execute the inner and outer loops until i<j that means the loop will execute till the complete array of numbers is scanned and if after complete scan i.e. one complete iteration i<j (index i still remains less than index j) then the numbers on corresponding indices will be swapped.
sample code
Solution(b):
As it can be clearly seen from the code snippet in part a solution that increment of i and decrement of j is dependent on the condition (i<j and a[i]<p) which means that the execution of these statements will depend on the input values given in the sequence list and not on the number of elements in the list.
The execution of statement i=i+1 or j=j-1 will be maximum when the array list is already sorted.
Example: {5,4,3,2,1,0} let p=0 (should be less than all elements in the list)
here i=i+1 will be executed n(5) times.
Similarly in {0,1,2,3,4,5} let p=0
here j=j-1 will be executed n times thus maximising the execution of these statements
Solution(C):
Solution(d):
The asymptotic lower bound of the time complexity is omega(n) where n is the number of elements. It will be when the list is sorted as explained in part b solution.
The worst-case complexity is necessary to determine the maximum time taken by the algorithm to execute. As it is clearly seen from the algorithm the number of times i or j incremented in the worst case that many numbers of swaps are possible as it is under outer while(i<j) loop.
PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT
SECTION
Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an...
What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...
What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...
Given the following algorithm: Algorithnm Input: a1, a2,...,an, a sequence of numbers n, the length of the sequence x, a number Output: ?? i:- 1 While (x2 # a, and i < n) i+1 End-while If (x- - a) Return(i) Return(-1) 3, -1, 2,9, 36,-7, 6,4 a) What is the correct output of the Algorithm with the following input: a1, a2,..an b) What is the asymptotic worst-case time complexity of the Algorithm? Algorithnm Input: a1, a2,...,an, a sequence of numbers...
Question 1: Complexity Take a look at the following algorithm written in pseudocode: procedure mystery(a1, a2, …, an: integer) i := 1 while (i < n and ai ≤ ai+1) i := i + 1 if i == n then print “Yes!” else print “No!” What property of the input sequence {an} does this algorithm test? What is the computational complexity of this algorithm, i.e., the number of comparisons being computed as a function of the input size n? Provide...
(V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a list of n integers) for i = 2 to n do c= j=i-1 while (j > 0) do if ra; then break end if 4j+1 = a; j= j-1 end while j+1 = 1 end for 14: return 0.02. 1, 15: end procedure Answer the following questions: (1) Run the algorithm with input (41, 02, 03, 04) = (3, 0, 1,6). Record the values...
Assume L is an array, length (L) returns the number of records in the array, and qsort \((L, \quad i, j)\) sorts the records of \(L\) from \(i\) to \(j\) (leaving the records sorted in L) using the Quicksort algorithm. What is the average-case complexity for the following code fragment?$$ \begin{array}{c} \text { for }(\mathrm{i}=0 ; \text { i<length }(\mathrm{L}) ; \mathrm{i}++) \\ \text { qsort }(\mathrm{L}, 0, \mathrm{i}) ; \end{array} $$You should provide a formula for computing the total...
pleas answer asap 3. (20 points) Algorithm Analysis and Recurrence There is a mystery function called Mystery(n) and the pseudocode of the algorithm own as below. Assume that n 3* for some positive integer k21. Mystery (n) if n<4 3 for i1 to 9 5 for i-1 to n 2 return 1 Mystery (n/3) Print "hello" 6 (1) (5 points) Please analyze the worst-case asymptotic execution time of this algorithm. Express the execution time as a function of the input...
What is the time-complexity of the algorithm abc? Procedure abc(n: integer) s := 0 i :=1 while i ≤ n s := s+1 i := 2*i return s consider the following algorithm: Procedure foo(n: integer) m := 1 for i := 1 to n for j :=1 to i2m:=m*1 return m c.) Find a formula that describes the number of operations the algorithm foo takes for every input n? d.)Express the running time complexity of foo using big-O/big-
A. What is the time complexity of Merge Sort? B. Explain why Merge Sort has the time complexity you listed above in Part A. Algorithm Quicksort (A, left, right) if (left < right) pivot Point = [(left+right)/2] 11 note central pivot i left - 1 ja right + 1 do do it i +1 while (i < A.size) and (A[i] SA[pivot Point]) do jj-1 while (j > i) and (A[il > A[pivot Point]) if (i <j) then swap (A[i], Aljl)...
For each problems segment given below, do the following: Create an algorithm to solve the problem Identify the factors that would influence the running time, and which can be known before the algorithm or code is executed. Assign names (such as n) to each factor. Identify the operations that must be counted. You need not count every statement separately. If a group of statements always executes together, treat the group as a single unit. If a method is called, and...