-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathMerge_Sort.py
58 lines (49 loc) · 1.15 KB
/
Merge_Sort.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# -*- coding: utf-8 -*-
"""
Author: Himanshu Agarwal
Github: himanshu-03 (http://github.com/himanshu-03)
LinkedIn: agarwal-himanshu (https://linkedin.com/in/agarwal-himanshu)
# Merge Sort
"""
def merger(a, start, mid, end):
temp = []
for i in range(len(a)):
temp.append(0)
i = start
j = mid + 1
ti = start
while i <= mid and j <= end:
if a[i] < a[j]:
temp[ti] = a[i]
ti += 1
i += 1
else:
temp[ti] = a[j]
ti += 1
j += 1
while i <= mid:
temp[ti] = a[i]
ti += 1
i += 1
while j <= end:
temp[ti] = a[j]
ti += 1
j += 1
for i in range(start, end+1):
a[i] = temp[i]
def mergesort(a, start, end):
if start < end:
mid = (start+end)//2
mergesort(a, start, mid)
mergesort(a, mid+1, end)
merger(a, start, mid, end)
a = []
size = int(input('Enter size of array: '))
print('')
for i in range(size):
data = int(input('Enter element: '))
a.append(data)
print('')
print('Elements are: ', a)
mergesort(a, 0, len(a)-1)
print('After sorting elements are: ', a)