Advertisement
brainuser5705

"inplace merge sort" with swapping

Apr 20th, 2021
89
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 swap(arr, arr_index, pointer_index):
  5.     temp = array2[pointer_index]
  6.     array2[pointer_index] = arr[arr_index]
  7.     arr[arr_index] = temp
  8.  
  9. def solve():
  10.     index1 = len(array1) - 1
  11.     index2 = len(array1) - 1
  12.     for i in range(len(array2) - 1, -1, -1):
  13.         if (array1[index1] > array2[index2]):
  14.             swap(array1, index1, i)
  15.             index1 -= 1
  16.             if (index1 < 0):
  17.                 index1 = 0
  18.         else:
  19.             swap(array2, index2, i)
  20.             index2 -= 1
  21.             if (index2 < 0):
  22.                 index2 = 0
  23.  
  24. solve()
  25. print(array2)
Advertisement
RAW Paste Data Copied
Advertisement