考虑以下实施斗排序:Bucket-Sort的这种实现是否被认为是“in-place”?
Algorithm BucketSort(S)
input: Sequence S of items with integer keys in range [0,N-1]
output: Sequence S sorted in nondecreasing order of keys.
let B be an array of N sequences, each of which is initially empty
for each item x in S do
let k be the key of x
remove x from S and insert it at the end of bucket (sequence) B[k].
for i←0 to N-1 do
for each item x in sequence B[i] do
remove x from B[i] and insert it at the end of S.
这是实现认为是“就地”?
我的教科书给出了“就地”的定义如下:
请记住,一个排序算法是就地 如果仅使用一个恒定 量的内存除了 需要被排序的对象。
现在,我知道上面的算法使用O(n + N)内存,其中N是范围的上限。然而,我可能错了,我认为N是一个常数,即使它是一个大的。所以我猜这是根据这个定义“就地”,但我不确定。
因此,考虑到上述算法和“原地”的定义,这种实现是否被认为是原地?
http://en.wikipedia.org/wiki/In-place_algorithm – 2009-09-27 21:37:22