Question: n a sequence S = [ S , S 2 , . . . , Sn ] of n integers, an inversion is a pair
n a sequence S S S Sn of n integers, an inversion is a pair of elements s and s where
i s For example, in the sequence
S
the pairs and are inversions.
An array with n elements may have as many as
nn
nn
the best
inversions. When the number of inversions, k may be any value between and
algorithm for counting inversions has running time n log n There also exists a On k
algorithm for counting inversions, which is n when k
Your goal in this lab is to create two algorithms which count the number of inversions in an input
sequence:
Input: An array A of n integers in the
range
to n
Output: An integer, corresponding to the number of inversions in A
The first algorithm will have a n log n runtime and will be better for counting inversions
when k n The second algorithm will have runtime n k and will be better when kn
ien nn
Bonus:
Time permitting, you should try to implement them.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
