两个升序排列的数组,合并之后仍然为升序排列。
1 num_list1 = [2, 4, 7, 9, 11, 12] 2 num_list2 = [3, 5, 6, 7, 8, 11, 16] 3 4 size1 = len(num_list1) 5 size2 = len(num_list2) 6 temp = [0] * (size1 + size2) 7 i, j, k = 0, 0, 0 8 while i < size1 and j < size2: 9 if num_list1[i] < num_list2[j]: 10 temp[k] = num_list1[i] 11 k += 1 12 i += 1 13 else: 14 temp[k] = num_list2[j] 15 k += 1 16 j += 1 17 while i < size1: 18 temp[k] = num_list1[i] 19 k += 1 20 i += 1 21 while j < size2: 22 temp[k] = num_list2[j] 23 k += 1 24 j += 1 25 print(temp)