Advertisement
brainuser5705

"inplace merge sort" with shifting (insertion sort)

Apr 20th, 2021
86
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. array1 = [1,6,10,11]
  2. array2 = [5,7,10,20,0,0,0,0]
  3.  
  4. def solve():
  5.     for num1 in array1:
  6.         index = 0
  7.         while array2[index] != 0 and num1 >= array2[index]:
  8.             index += 1
  9.         shift(index, len(array2)-1, 1)
  10.         array2[index] = num1
  11.  
  12. def shift(start_index, end_index, num_shifts):
  13.     for i in range(end_index-1, start_index-1, -1):
  14.         array2[i+num_shifts] = array2[i]
  15.  
  16. def main():
  17.     solve()
  18.     print(array2)
  19.  
  20. main()
Advertisement
RAW Paste Data Copied
Advertisement