mergeSortedArrays_optimized

Run Settings
LanguagePython
Language Version
Run Command
# mergeSortedArrays([0,3,4,31], [4,6,30]); # [0,3,4,4,6,30,31] # inputs: two arrays # outputs: one merged array # option1: append the array, sort the resulting array o(n²) # option2: optimization def mergeSortedArrays(arr1, arr2): # error handling: # arr_sum = arr1 + arr2 # O(n) # return sorted(arr_sum) idx_1 = 0 idx_2 = 0 sorted_array = list() while (idx_1 + idx_2) < (len(arr1)+len(arr2)): if arr1[idx_1] < arr2[idx_2]: sorted_array.append(arr1[idx_1]) if idx_1 < (len(arr1)-1): idx_1+=1 print(f"idx1: {idx_1}") else: sorted_array.append(arr2[idx_2]) if idx_2 < (len(arr2)-1): idx_2+=1 print(f"idx2: {idx_2}") return sorted_array arrayExample_1 = [0,3,4,31] arrayExample_2 = [4,6,30] print(mergeSortedArrays(arrayExample_1, arrayExample_2))
Editor Settings
Theme
Key bindings
Full width
Lines