-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode4.py
More file actions
38 lines (29 loc) · 735 Bytes
/
Code4.py
File metadata and controls
38 lines (29 loc) · 735 Bytes
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
#Code given below running successfully and it gives a correct output
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
merge_sort(left)
merge_sort(right)
i = j = k = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
while i < len(left):
arr[k] = left[i]
i += 1
k += 1
while j < len(right):
arr[k] = right[j]
j += 1
k += 1
arr = [30,50,55,46,100,19]
merge_sort(arr)
print(f"The sorted array is: {arr}")