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
Get step-by-step solutions from verified subject matter experts
