Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import random
- # The program sorts the list of numbers in the form of waves.
- # It means a list for which the following condition
- # is true a1> = a2 <= a3> = a4 <= ...
- def merge_sort(l):
- """Recursive merge sort."""
- if len(l) > 1:
- mid = len(l) // 2
- left = l[:mid]
- right = l[mid:]
- merge_sort(left)
- merge_sort(right)
- i = j = k = 0
- while i < len(left) and j < len(right):
- if left[i] < right[j]:
- l[k] = left[i]
- i += 1
- else:
- l[k] = right[j]
- j += 1
- k += 1
- if i < len(left):
- l[k: k + len(left[i:])] = left[i:]
- if j < len(right):
- l[k: k + len(right[j:])] = right[j:]
- n = int(input('Enter the length of the list (positive integer):\n >'))
- lst = random.sample(range(n * 10), n)
- print("Input list: {}".format(lst))
- merge_sort(lst)
- arr = []
- i = 0
- tmp = 0
- while i != n - 1:
- if i % 2 == 0:
- tmp = lst[i]
- arr.append(lst[i+1])
- else:
- arr.append(tmp)
- i += 1
- print("Output list: ", arr)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement