Advertisement
Falexom

Untitled

Jul 23rd, 2021
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. def shell(arr):
  2.     gap = len(arr) // 2
  3.  
  4.     while gap > 0:
  5.         for i in range(gap, len(arr)):
  6.             current = arr[i]
  7.             ind = i
  8.  
  9.             while ind >= gap and arr[ind - gap] > current:
  10.                 arr[ind] = arr[ind - gap]
  11.                 ind -= gap
  12.                 arr[ind] = current
  13.  
  14.         gap //= 2
  15.     return arr
  16.  
  17.  
  18. arr = [12, 74, 25, 14, 60, 28, 13, 5, 76]
  19. print(arr)
  20. result = shell(arr)
  21. print(result)
  22.  
  23.  
  24. def merge_cut(lst):
  25.     if len(lst) <= 1:
  26.         return lst
  27.     middle = len(lst) // 2
  28.     left = lst[:middle]
  29.     right = lst[middle:]
  30.  
  31.     left = merge_cut(left)
  32.     right = merge_cut(right)
  33.  
  34.     return list(merge(left, right))
  35.  
  36.  
  37. def merge(left_half, right_half):
  38.     res = []
  39.     while len(left_half) != 0 and len(right_half) != 0:
  40.         if left_half[0] < right_half[0]:
  41.             res.append(left_half[0])
  42.             left_half.remove(left_half[0])
  43.         else:
  44.             res.append(right_half[0])
  45.             right_half.remove(right_half[0])
  46.     if len(left_half) == 0:
  47.         res += right_half
  48.     else:
  49.         res += left_half
  50.     return res
  51.  
  52.  
  53. lst = [12, 34, 25, 15, 67, 23, 11, 5, 86]
  54. print(lst)
  55. re = merge_cut(lst)
  56. print(re)
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement