Question: Algorithm inversions(A, n) Input: Array A has n Items Output: Number of inversions in A count = 0 for i from 1 to n-1 do

Algorithm inversions(A, n)

Input: Array A has n Items

Output: Number of inversions in A

count = 0

for i from 1 to n-1 do

cur = A[i]

j = i-1

while j>=0 and A[j] > cur do

count++

A[j+1] = A[j]

j--

A[j+1] = cur

return count

a. if A =[1,2,3,4,5] what does inversions(A) return?

b. if A =[2,3,1,4,5] what does inversions(A) return?

c. What is the exact miniumum number of times the line "count++" executes in terms of n

d. What is the exact maximum number of times the line "count++" executes in terms of n

e. Best case time complexity

f. worst case time complexity

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!