我设计一个算法来做到以下几点:鉴于阵列 A [1 ... N] ,每一个 I< Ĵ,找到所有反转对这样 A [1]> A [J] 。我使用的是归并排序和复制数组A阵列B,然后比较这两个数组,但我有困难的时候看我怎么可以用它来寻找反转的数目。任何提示或帮助将是很大的AP preciated。
I'm designing an algorithm to do the following: Given array A[1... n], for every i < j, find all inversion pairs such that A[i] > A[j]. I'm using merge sort and copying array A to array B and then comparing the two arrays, but I'm having a difficult time seeing how I can use this to find the number of inversions. Any hints or help would be greatly appreciated.
推荐答案我可以给的(这看起来很像一门功课的问题;)的唯一建议)是先手工用小组数字做(例如: 5),然后记下的步骤,你把要解决的问题。
The only advice I could give to this (which looks suspiciously like a homework question ;) ) is to first do it manually with a small set of numbers (e.g. 5), and then write down the steps you took to solve the problem.
这应该让你找出一个通用的解决方案,你可以用它来写code。
This should allow you to figure out a generic solution you can use to write the code.
更多推荐
以阵列计数反转
发布评论