-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergesort.py
44 lines (34 loc) · 913 Bytes
/
mergesort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def mergesort(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_arr = arr[:mid]
right_arr = arr[mid:]
mergesort(left_arr)
mergesort(right_arr)
i = j = k = 0
while i < len(left_arr) and j < len(right_arr):
if left_arr[i] < right_arr[j]:
arr[k] = left_arr[i]
i += 1
else:
arr[k] = right_arr[j]
j += 1
k += 1
while i < len(left_arr):
arr[k] = left_arr[i]
i += 1
k += 1
while j < len(right_arr):
arr[k] = right_arr[j]
j += 1
k += 1
return arr
n = int(input("Enter the number of elements: "))
arr = []
for i in range(n):
a = int(input("Enter element: "))
arr.append(a)
print()
print("Unsorted Array:", arr)
mergesort(arr)
print("Sorted Array:", arr)