[英]An array with O(n) inversions
Trying to figure out what type of an array consists of at most n inversions with n being the array size. 试图弄清楚什么类型的数组最多包含n个反转,其中n是数组大小。 I was thinking an array that is nearly sorted would fall under this case and also an array that is almost completely sorted with the max element and min element switched, for instance..
我在想一个几乎排序的数组将属于这种情况,并且还是一个几乎完全按照max元素和min元素切换排序的数组,例如..
9 2 3 4 5 6 7 8 1 9 2 3 4 5 6 7 8 1
So my thinking is that when an array has at most n inversions, is it safe to say that the array is nearly sorted? 所以我的想法是,当一个数组最多有n个反转时,可以说这个数组几乎是排序的吗? Or are there other cases where the array would have at most n inversions and not be nearly sorted.
或者是否存在其他情况,其中数组最多具有n个反转并且几乎不进行排序。
The 'least' sorted array (ie reverse sorted) has 1 + 2 + 3 + ... + n-1 = n(n-1)/2
inversions. “最少”排序的数组(即反向排序的)具有
1 + 2 + 3 + ... + n-1 = n(n-1)/2
反转。
The less inversions an array has, the 'more' sorted it is. 数组的反转越少,它就越“排序”。
And, since n
is quite a bit smaller than n(n-1)/2
, one can probably call an array with n
inversions 'nearly sorted'. 并且,由于
n
比n(n-1)/2
小很多,因此可以将具有n
反转的数组称为“近似排序”。
This array has n-1
inversions: 此数组具有
n-1
反转:
9 1 2 3 4 5 6 7 8
In response to your comment, insertion sort 's complexity is O(n + d)
, where d
is the number of inversions, thus it will run in O(n)
for O(n)
inversions. 响应您的注释, 插入排序的复杂度为
O(n + d)
,其中d
是反转次数,因此它将在O(n)
以O(n)
反转运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.