Question

In: Computer Science

Given the below code, compute the values of the best case, worst case and average case...

Given the below code, compute the values of the best case, worst case and average case knowing that the basic operation in the first loop takes 4 ns to execute and the basic operation in the second loop takes 5 ns to execute.

int A[20];

for (int i=0; i < 20; i++)

{

          cin >> A[i];

}

for (i=0; i < 20; i++)

{

          if (A[i] % 3 == 0)

                    break;

}

Solutions

Expert Solution

The first loop inputs 20 elements, thus the total time for the first loop

= 20 * (4 ns)

= 80 ns

Now, following are the possibilities for second loop :

  • Best Case :
    • This happens when the condition matches with A[0] (first element itself), and loop breaks.
    • Total time in this case
      • = 1 * (5 ns) = 5 ns
  • Worst Case :
    • This happens when no element matches the condition, and the loop executes over each of the 20 elements.
    • Total time in this case
      • = 20 * (5 ns) = 100 ns
  • Average Case :
    • In this case, the loop may exit after
      • 1st element, taking time 1 * (5 ns) = 5 ns
      • 2nd element, taking time 2 * (5 ns) = 10 ns
      • ..
      • ..
      • 20th element, taking time 20 * (5 ns) = 100 ns
    • Average time in this case

= 52.5 ns

Thus, total times in

  • Best Case
    • = 80 ns + 5 ns = 85 ns
  • Worst Case
    • = 80 ns + 100 ns = 180 ns
  • Average Case
    • = 80 ns + 52.5 ns = 132.5 ns

Related Solutions

merge sort Determine the best, average, and worst case inputs for merge sort.
merge sort Determine the best, average, and worst case inputs for merge sort.
Analyze the worst-case, best-case, and average-case number of comparisons of sequential search if exactly three-tenths of...
Analyze the worst-case, best-case, and average-case number of comparisons of sequential search if exactly three-tenths of the time, the element x to search for is not in the list and if x is in the list, it is equally likely to be in any position.
6. List and explain the worst-case and average-case running times for each LinkedList method below: (a)...
6. List and explain the worst-case and average-case running times for each LinkedList method below: (a) insert(iterator here, Object item) (b) insertAtHead (c) insertAtTail (aka push back) (d) get(iterator here) (e) get(index i) (f) remove(iterator here) (g) remove(index i) (h) splice(iterator place here, iterator from here, iterator to here) 7. When should you use a Vector, and when should you use a Linked List?
20. The concept of best-, worst-, and average-case analyses extends beyond algorithms to other counting problems...
20. The concept of best-, worst-, and average-case analyses extends beyond algorithms to other counting problems in mathematics. Recall that the height of a binary tree is the number of edges in the longest path from the root to a leaf. (a) Find the best-case height of a binary tree with five nodes. (b) Find the worst-case height of a binary tree with five nodes. (c) Find the average-case height of a binary tree with five nodes. For this problem,...
How to compute the Z, N, C, and Z the condition code bit values? Given A...
How to compute the Z, N, C, and Z the condition code bit values? Given A and B, and after subtracting how do I find the condition codes? Please provide a few examples with the explanation.
What is the best case and worst case performance for a hashtable lookup explain answer very...
What is the best case and worst case performance for a hashtable lookup explain answer very clearly and answer must be explained how hashtables work and how that relates to performance in both the cases.
How many key comparisons are made by mergesort in the a. Best case? b. Worst Case?
How many key comparisons are made by mergesort in the a. Best case? b. Worst Case?
Show that the worst-case and average-case time complexities for the number of assignments of records performed...
Show that the worst-case and average-case time complexities for the number of assignments of records performed by the Exchange Sort algorithm (Algorithm 1.3) are given by           W(n) = 3n(n-1)/2 ≈ n2/2 and A(n) = 3n(n-1)/4 ≈ n2/4
QUESTION 8 In a base-case scenario, the output is determined by assuming a. worst values that...
QUESTION 8 In a base-case scenario, the output is determined by assuming a. worst values that can be expected for the random variables of a model. b. the most likely values for the random variables of a model. c. the mean trial values for the random variables of a model. d. best values that can be expected for the random variables of a model. e. None of the above 1.5 points    QUESTION 9 The points where constraints intersect on...
Accountants are counted on to provide management with analyzing data to determine best- and worst-case scenarios....
Accountants are counted on to provide management with analyzing data to determine best- and worst-case scenarios. As future planning becomes more complex, these what-if analyses can increase in complexity and usefulness. Identify and discuss at least three (3) types of what-if analyses that an accountant should be able to perform to measure a firm’s performance over a period. Be sure to include the type of data that will be needed to support this analysis. Justify your response.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT