我在Python
中遇到了問題Count inversions in the array的解決方案,它使用「非比較排序算法來計算」。它對執行按位操作的代碼進行2次遞歸調用。這是用於計算數組O(n)複數中的倒數的算法嗎?
def inv_count(a, m=(1 << 32)):
if not m or not a:
return 0
count = 0
ones, zeros = [], []
for n in a:
if n & m:
ones.append(n & ~m)
else:
count += len(ones)
zeros.append(n & ~m)
m /= 2
return count + inv_count(ones, m) + inv_count(zeros, m)
print inv_count([1, 2, 3, 4, 5])
print inv_count([2, 4, 1, 3, 5])
print inv_count([5, 4, 3, 2, 1])
此解決方案是否真正線性O(n)
?
相似,所以問題聲稱這是不可能的(Is there an algorithm to count array inversions in linear time?),雖然有很多文獻上分而治之算法(與O(n logn)
複雜性),我會感謝有關上述解決方案的一些參考。
這可以幫助嗎? http://stackoverflow.com/questions/337664/counting-inversions-in-an-array - 有一些'O(n日誌n)'解決方案,我認爲這表明你是最低的可以去... –
您發佈的算法不是線性'O(n)',因爲它是遞歸調用'inv_count(ones,m)'。它是'O(n * log n)'。 – Onheiron
嗯,這很有趣。我在'Python'中做了一個簡單的timeit腳本,它顯示出一個經典的「分而治之」的解決方案比使用按位操作...:https://gist.github的代碼快得多(2-3倍)。 com/oskar-j/f9bb24e4c390cedd3216 – oski86