📜  合并3个排序的数组(1)

📅  最后修改于: 2023-12-03 15:37:09.843000             🧑  作者: Mango

合并3个排序的数组

在编程中,我们常常需要合并多个排序的数组。本文将向大家介绍如何合并3个排序的数组。我们将运用归并排序的思路,将3个数组一分为二,分别进行归并操作,最终合并为一个有序数组。

算法思路
  1. 对3个数组分别进行归并排序操作,操作结束后,将它们分成2个已排序的数组。
  2. 对这2个数组再进行归并排序操作,操作结束后,合并这2个已排序数组为一个有序数组。
  3. 返回已排序的数组。
示例代码
def merge_sorted_arrays(arr1, arr2, arr3):
    n1, n2, n3 = len(arr1), len(arr2), len(arr3)
    sorted_arr1 = []
    sorted_arr2 = []
    sorted_arr3 = []
    index1 = 0
    index2 = 0
    index3 = 0

    # 先对三个数组进行归并排序
    while index1 < n1 and index2 < n2 and index3 < n3:
        min_val = min(arr1[index1], arr2[index2], arr3[index3])
        if arr1[index1] == min_val:
            sorted_arr1.append(min_val)
            index1 += 1
        elif arr2[index2] == min_val:
            sorted_arr2.append(min_val)
            index2 += 1
        else:
            sorted_arr3.append(min_val)
            index3 += 1
            
    while index1 < n1 and index2 < n2:
        if arr1[index1] < arr2[index2]:
            sorted_arr1.append(arr1[index1])
            index1 += 1
        else:
            sorted_arr2.append(arr2[index2])
            index2 += 1
            
    while index1 < n1 and index3 < n3:
        if arr1[index1] < arr3[index3]:
            sorted_arr1.append(arr1[index1])
            index1 += 1
        else:
            sorted_arr3.append(arr3[index3])
            index3 += 1
            
    while index2 < n2 and index3 < n3:
        if arr2[index2] < arr3[index3]:
            sorted_arr2.append(arr2[index2])
            index2 += 1
        else:
            sorted_arr3.append(arr3[index3])
            index3 += 1

    while index1 < n1:
        sorted_arr1.append(arr1[index1])
        index1 += 1

    while index2 < n2:
        sorted_arr2.append(arr2[index2])
        index2 += 1

    while index3 < n3:
        sorted_arr3.append(arr3[index3])
        index3 += 1

    # 将分别排序后的三个数组合并为一个已排序的数组
    merged_arr = []
    index1 = 0
    index2 = 0
    index3 = 0
    n1, n2, n3 = len(sorted_arr1), len(sorted_arr2), len(sorted_arr3)

    while index1 < n1 and index2 < n2 and index3 < n3:
        min_val = min(sorted_arr1[index1], sorted_arr2[index2], sorted_arr3[index3])
        merged_arr.append(min_val)
        if sorted_arr1[index1] == min_val:
            index1 += 1
        elif sorted_arr2[index2] == min_val:
            index2 += 1
        else:
            index3 += 1

    while index1 < n1 and index2 < n2:
        if sorted_arr1[index1] < sorted_arr2[index2]:
            merged_arr.append(sorted_arr1[index1])
            index1 += 1
        else:
            merged_arr.append(sorted_arr2[index2])
            index2 += 1

    while index1 < n1 and index3 < n3:
        if sorted_arr1[index1] < sorted_arr3[index3]:
            merged_arr.append(sorted_arr1[index1])
            index1 += 1
        else:
            merged_arr.append(sorted_arr3[index3])
            index3 += 1

    while index2 < n2 and index3 < n3:
        if sorted_arr2[index2] < sorted_arr3[index3]:
            merged_arr.append(sorted_arr2[index2])
            index2 += 1
        else:
            merged_arr.append(sorted_arr3[index3])
            index3 += 1

    while index1 < n1:
        merged_arr.append(sorted_arr1[index1])
        index1 += 1

    while index2 < n2:
        merged_arr.append(sorted_arr2[index2])
        index2 += 1

    while index3 < n3:
        merged_arr.append(sorted_arr3[index3])
        index3 += 1

    return merged_arr

以上是合并3个排序的数组的示例代码,希望对你有所帮助。