We know that several sorts, such as insertion sort, are great on arrays that are ‘mostly-sorted’ and not so great on random data.
Suppose we wanted to profile the performance improvement/degradation of such an algorithm relative to how ‘sorted’ the input data is. What would be a good way to generate an ‘increasingly sorted’ or ‘increasingly random’ array of elements? How might we measure the ‘sortedness’ of the input?
Number of Inversion is a usual measure of how much sorted an array is.
A pair of elements
(pi,pj)in permutation p is called an inversion in a permutation ifi<jandpi >pj. For example, in the permutation(3,1,2,5,4)contains the 3 inversions(3,1),(3,2)and(5,4).A sorted array got 0 inversion and reverse sorted array got n*(n-1)/2.