0

I am trying to count inversion in a array (two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j). I know that is easily possible to resolve these problems using brute force in O(n^2) and by using Divide and Conquer in O(nlgn).

My Question was that is it possible to use a form of bucketing technique to achieve an efficiency of O(n) with knowledge about the data. For instance I already know that the array is a permutation of 1-32 , thus the maximum element is 32 (entailing that we can do something with bucketing).

I have been thinking about this and noticed that if we are inserting an element in a bucket, then the sum of all buckets greater than it at the time of insertion is its inversion count. But if we add the number of elements in each bucket every time, then it causes me to lose the O(n) efficiency. Any suggestions of how to keep a count to remove this penalty.

Please note that the permutation can be of any length, but during execution we know the number of elements in the permutation. Thus the value of "n" is known during execution and the permutation consists of elements from "1" to "n".

Sorting : It is possible to sort this data set in O(n) time complexity, as we can create 32 buckets and we know that each bucket will have exactly one element. Thus the efficiency of bucket sort which is O(n + M) is O(n + 1) = O(n) for this specific example.

chettyharish
  • 1,704
  • 7
  • 27
  • 41
  • Not that sure but complexity that is big-O depends on input size. But if the input size itself becomes constant doesn't it mean that complexity is also constant ? – advocateofnone Mar 08 '15 at 06:52
  • The n could be anything, but I know that it is a permutation of size n. i.e the elements in the array are from 1 to n . And I also know n for the execution. – chettyharish Mar 08 '15 at 06:54
  • ok i thought n was always 32 makes sense now – advocateofnone Mar 08 '15 at 06:56
  • 1
    I http://stackoverflow.com/questions/6024614/optimal-inversion-counting-on-int-arrays have a look at this not exactly what your are looking for but might be helpful – advocateofnone Mar 08 '15 at 07:10
  • A permutation of the numbers 1..32 has size 32, so n is constant too. – Niklas B. Mar 08 '15 at 10:26

1 Answers1

2

According to http://arxiv.org/pdf/1503.01192.pdf it is "well known" that you cannot find the number of inversions more efficiently than O(n log n).

Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • 2
    I guess one simple way to prove this would be to start from the proposition that it is impossible to sort more efficiently (in the worst case) than O(nlogn), and note that the problem of sorting an array can be reduced to the problem of identifying all inversions. – Asad Saeeduddin Mar 08 '15 at 07:35
  • It is possible to sort this data set in O(n) time since we know that bucket sort works in O(M + n ) and here M = 1, as exactly one element maps to each bucket. – chettyharish Mar 08 '15 at 19:59