以阵列计数反转 [英] Counting inversions in an array

查看:86
本文介绍了以阵列计数反转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计一个算法来做到以下几点:鉴于阵列 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.

这篇关于以阵列计数反转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆