Question

Question 2 Consider the following algorithm Fun that takes array A and key Kas Fun(AO,...,n - 1], K) count = 0 for i = 0 ton
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Θ(n2)

The variable i goes from 0 to n-1. The variable j goes from i+1 to n-1. So for i=0, j goes from 1 to n-1 or n-1 iterations, for i=1, j goes from 2 to n-1 or n-2 iterations, up to i=n-1 when j goes from n to n-1, which is 0 iterations since starting point is greater than ending point. So, the total number of iterations is (n-1)+(n-2)+....+1+0 = (n-1)(n-1+1)/2 = n(n-1)/2 = (n2-n)/2. In the best case of the function, the if-condition will always be false and none of the operations will be executed. So, only the comparison in the condition of the if will be executed inside the loop. So, it will have a total of (n2-n)/2 operations in the best case. So, the best case time complexity is Θ(n2) since the highest power of n is always selected.

Add a comment
Know the answer?
Add Answer to:
Question 2 Consider the following algorithm Fun that takes array A and key Kas Fun(AO,...,n -...
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
  • #9 What is time complexity of fun()? int fun(int n) {   int count = 0;   for...

    #9 What is time complexity of fun()? int fun(int n) {   int count = 0;   for (int i = n; i > 0; i /= 2)      for (int j = 0; j < i; j++)         count += 1;   return count; } Group of answer choices O(n^2) O(nLogn) O(n) O(nLognLogn)

  • Perform the following to the algorithm below: - - Express T(n) as a function of n...

    Perform the following to the algorithm below: - - Express T(n) as a function of n Find a best approximation for the Big O function for T(n) Perform a time complexity analysis Define the basic operation of the algorithm Correctness Efficiency - - Procedure maxMin (n, A, I, h) integer h, I, A (1:n), n integer j j-2 IA (1) hS (1) while (i <=n) do if (Ali) < 1) then TEA (0) if(Ali) >h) then h A() j+į+1 repeat...

  • Determine the worst-case complexity of the algorithm in terms of n. // search a key in...

    Determine the worst-case complexity of the algorithm in terms of n. // search a key in an array a [1..n] of length n for(int k = 1; k <= n; k++)      if(a[k] == key) then return k; return -1; // meaning key not found

  • Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j...

    Let A = [A[1], A[2],…..,A[n]] be an array of n distinct integers. For 1 <= j <= n, the index j is a happy index if A[i] < A[j] for all 1 <= i < j. Describe an O(n)- time algorithm that finds all the happy indices in the array A. Partial credit will be given for an O(n log(n))-time algorithm and a minimal credit will be given for an O(n^2) –time algorithm. What is the running time of your...

  • (V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a...

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

  • Algorithm ComparisonCountingSorting(A[0..n-1], S[0..n-1] )          //Input: Array A[0..n-1] of orderable values          //Output: Array S[0..n-1] of...

    Algorithm ComparisonCountingSorting(A[0..n-1], S[0..n-1] )          //Input: Array A[0..n-1] of orderable values          //Output: Array S[0..n-1] of A's values in ascending order   for i ← 0 to n – 1 do Count[i] ← 0         for i ← 0 to n – 2 do for j ← i + 1 to n – 1 do                      if A[i] < A[j]    Count[j] ← Count[j] + 1      // increment counter of larger else Count[i] ← Count[i] + 1   for i ← 0 to n –...

  • Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a...

    Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a and b are integers while (a>0) B- a/2 A a-2 end while return b; i. What is the time complexity of the IterativeFunction pseudocode shown above? ii. What is the space complexity of the IterativeFunction pseudocode shown above? 2. What is the time complexity of the following algorithm (Note that n(n+1) 2,2 n(n+1)(2n+1) 2 and ): Provide both T(n) and order, e(f(n)). int A=0;...

  • 1. Consider the following well-known sorting algorithm, which is studied later in the book, with a...

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

  • Implement a binary search function that takes an integer query key and determines whether it is...

    Implement a binary search function that takes an integer query key and determines whether it is in a sorted array of n integers. It should return the index of the key, if found, otherwise it should return a negative number. You should implement the function both iteratively and recursively to be sure you really understand the algorithm. Just for the fun of it, you might want to verify the O(log(n)) complexity of your function by counting the number of comparisons...

  • Let A[1..n] be an array with n elements. Consider the Count-Occurrence algorithm with the pseudocode below....

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

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