排序列表数组问题

时间:2010-09-21 11:32:10

标签: arrays sorting

是否有可能使用n / 2大小的工作来合并两个大小为n / 2的排序列表?array ???

2 个答案:

答案 0 :(得分:0)

这对复杂度O(n)没有影响。 你无法一直得到n / 2。 A = [1,2,3,10] B = [4,5,6,8]

你需要n-1。

答案 1 :(得分:0)

如果你的意思是在最终数组之上和之上所需的额外存储中使用数组,你甚至不需要它。要对已经排序的两个列表进行排序,您只需要合并它们,并且只需要两个值(每个源列表一个)而不是n/2

算法如下:

create empty list newlist
set idx1 to start of list1
set idx2 to start of list2
while idx1 within list1 or idx2 within list2:
    if idx1 beyond list1:
        append list2[idx2] to newlist
        increment idx2
    else if idx2 beyond list2:
        append list1[idx1] to newlist
        increment idx1
    else if list1[idx1] is less than list2[idx2]:
        append list1[idx1] to newlist
        increment idx1
    else:
        append list2[idx2] to newlist
        increment idx2

如果您的意思是“您可以将两个n/2大小的数组合并到另一个n/2大小的数组中吗?”那么,你可以把两个元素组合成一个(例如将16位整数拖到32位元素中)做一些诡计,不,这是不可能的。

相关问题