algorithm - refine merge-sort to count number of inversions -
i'm working on improved version of merge sort
counts number of inversions
.
note: don't want homework done, have ideas , love know whether make sense @ all.
as merge-sort o(nlogn
need adjust without worsing overall performance. think have insert test takes constant time (1)
(1) test inversion (upper) a[i] > a[j] given i<j
to find appropriate spot insert test asking myself: when during execution of merge sort guaranteed test runs unrelated n?
i think spot true when the list split one-element lists
hence, propose following algorithm adjustment
use divide-and-conquer , split list 1 elements lists test a[i] > a[j] i<j true 2 adjacent lists
does make sense @ all?
eventually, have carry count of inversions until algorithm terminates. give me hint regard problem?
(intuitively give count merge part algorithm terminates when merging complete.)
cheers, andrew
if count when array size 1, you're looking @ pair-wise inversions. think want keep counter throughout recursive calls, , increment whenever, during merge step, compare element left subarray 1 right subarray , 1 right smaller (assuming you're sorting in ascending order).
Comments
Post a Comment