Question
The following algorithm adds all the entries in a square n n array A. Analyze this algorithm where the work unit is the addition operation.
The following algorithm adds all the entries in a square n n array A. Analyze this algorithm where the work unit is the addition operation. sum = 0 for i = 1 to n do for j = 1 to n do sum = sum + A[i, j] end for end for write (Total of all array elements is, sum)
5. Analyze the following algorithm where the work unit is the output statement. Assume that n = 2m for some positive integer m. integer j, k for k = 1 to n do j = n; while j 2 do write j j = j/2 end while end for
5. In one part of algorithm SelectionSort, the index of the maximum item in a list must be found. This requires comparisons between list elements. In an n-element (unsorted) list, how many such comparisons are needed in the worst case to find the maximum element? How many such comparisons are needed in the average case?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started