简体   繁体   English

如何在 Python 中对两个向量进行排序?

[英]How to sort two vectors in one in Python?

I need to write a function that receives two arrays (A[] and B[]) already sorted in ascending order, its function allocates a array C[] exactly with sum of the sizes of A and B, and interleaves the elements of A[] and B[] in C[], so that array C[] is ordered in ascending order.我需要编写一个函数来接收已经按升序排序的两个数组(A[] 和 B[]),它的函数准确地分配一个数组 C[] 与 A 和 B 的大小之和,并交错 A 的元素[] 和 B[] 在 C[] 中,因此数组 C[] 按升序排列。 Write function as efficiently as possible.尽可能高效地编写函数。 It is not to join the arrays and to order the array C[] using the Bubble method nor the Insertion.不是使用 Bubble 方法或 Insertion 来连接数组和对数组 C[] 进行排序。

Example: A[] = { 1, 3, 6, 7} and B[] = {2, 4, 5}, the new vector is C[] = { 1, 2, 3, 4, 5, 6, 7}示例:A[] = { 1, 3, 6, 7} 和 B[] = {2, 4, 5},新向量为 C[] = { 1, 2, 3, 4, 5, 6, 7 }

My code don't stop to run, what am I doing wrong?我的代码不停地运行,我做错了什么?

def union(v1, v2):
    c = [0, 0, 0, 0, 0, 0, 0, 0]
    auxv1 = 0
    auxv2 = 0
    i = 0
    j = 0
    k = 0
    while i < len(v1)-1:
        auxv1 = v1[i]
    while j < len(v2)-1:
        auxv2 = v2[j]
    while k < len(c):
        if auxv1 < auxv2:
            c[k] = auxv1
            i += 1
            k += 1
        else:
            c[k] = auxv2
            j += 1
            k += 1
    if i == len(v1)-1 and j == len(v2)-1:
        return c

You may just iterate over the two lists and append to the list c an element of a or of b according to which one is smaller.您可以遍历两个列表,然后根据哪个元素较小,将ab的元素附加到列表c
An example is as follows.一个例子如下。

c=[]
i = j = 0

while True:
    if j == len(b):
        c+=a[i:]
        break
    elif i == len(a):
        c+=b[j:]
        break
    elif a[i] < b[j]:
        c.append(a[i])
        i+=1
    else:
        c.append(b[j])
        j+=1

The time complexity of this approach is linear with respect to the length of the two lists.这种方法的时间复杂度与两个列表的长度linear关系。

@newbie has written a very clear algorithm that is also very fast -- the number of list operations it makes is exactly equal to the number of elements in the result, ie linear. @newbie 写了一个非常清晰的算法,它也非常快——它进行的列表操作的数量正好等于结果中的元素数量,即线性。

You can find a sub-linear algorithm though, by looking at runs of values from one list, that should be inserted before the next value from the other list.但是,您可以通过查看一个列表中的值运行来找到一种次线性算法,该算法应该插入到另一个列表中的下一个值之前。

I'm guessing this can perform faster on longer lists with large runs.. (as always when it comes to performance you'll need to test it, with your own data).我猜这可以在较大运行的较长列表上执行得更快..(与往常一样,在性能方面,您需要使用自己的数据对其进行测试)。

a = [1,3,6,7]
b = [2,4,5,9,11,12,13,14,15,16]
c = []

a1 = a2 = b1 = b2 = 0
alen = len(a)
blen = len(b)
clen = alen + blen

while a2 + b2 < clen:
    print "processed:", a2 + b2
    if b2 == blen:    # done with b..
        c += a[a1:]   # just append rest of a
        break         # and we're done
    else:
        # find indexes a1..a2 such that all items come before the next b
        next_b = b[b2]
        while a2 < alen and a[a2] < next_b:
            a2 += 1
        c += a[a1:a2]   # extend c with the chunk we've found
        a1 = a2

    if a2 == alen:    # we're done with a
        c += b[b1:]   # just append rest of b
        break         # and we're done
    else:
        next_a = a[a2]
        while b2 < blen and b[b2] < next_a:
            b2 += 1
        c += b[b1:b2]
        b1 = b2

print c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM