-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmergesorting.py
45 lines (38 loc) · 974 Bytes
/
mergesorting.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
def mergesort(A, left, right):
if left < right:
mid = (left + right) // 2
mergesort(A, left, mid)
mergesort(A, mid + 1, right)
merge(A, left, mid, right)
def merge(A, left, mid, right):
i = left
j = mid + 1
k = left
B = [0] * (right + 1)
while i <= mid and j <= right:
if A[i] < A[j]:
B[k] = A[i]
i = i + 1
else:
B[k] = A[j]
j = j + 1
k = k + 1
while i <= mid:
B[k] = A[i]
i = i + 1
k = k + 1
while j <= right:
B[k] = A[j]
j = j + 1
k = k + 1
for x in range(left, right + 1):
A[x] = B[x]
A = []
v = int(input("Enter the size of an array : "))
for arr in range(v):
print("Enter the element at index : ",arr)
l = int(input())
A.append(l)
print('Original Array:',A)
mergesort(A, 0, len(A) - 1)
print('Sorted Array:',A)