简体   繁体   中英

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. 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.

Example: A[] = { 1, 3, 6, 7} and B[] = {2, 4, 5}, the new vector is 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.
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.

@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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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