python3合并排序

list1 = [21, 44, 50, 88, 999]
list2 = [99, 11, 24, 19, 999]
list3 = []


def select_sort(data, size):
    for base in range(size - 1):
        small = size
        for j in range(base + 1, size):
            if data[j] < data[small]:
                small = j
        data[small], data[base] = data[base], data[small]


def my_merge():

    global list1
    global list2
    global list3
    index1 = 0
    index2 = 0
    for index3 in range(len(list1) + len(list2) - 2):
        if list1[index1] < list2[index2]:
            list3.append(list1[index1])
            index1 += 1
            print('数字%d取自list1' % list3[index3])
        else:
            list3.append(list2[index2])
            index2 += 1
            print('数字%d取自list1' % list3[index3])
        print('合并结果', end='')
        for i in range(index3 + 1):
            print(list3[i], '', end='')
        print('\n')


def merge_sort():
    global list1
    global list2
    global list3

    select_sort(list1, len(list1) - 1)
    select_sort(list2, len(list2) - 1)

    print('\n list1排序结果', end='')
    for i in range(len(list1) - 1):
        print(list1[i], '', end='')
    print('\n list2排序结果', end='')
    for i in range(len(list2) - 1):
        print(list2[i], '', end='')
    print('\n')
    for i in range(60):
        print('=', end='')
    print()
    my_merge()
    for i in range(60):
        print('=', end='')
    print()
    print('\n 最终结果', end='')
    for i in range(len(list1) + len(list2) - 2):
        print('%d' % list3[i], end='')


merge_sort()

上一篇:Java List和Array之间的转换


下一篇:集合Collection接口与ArrayList与及其背后的数据结构