Day 27 of 100 Days of Python
Practising DSA
Today, I practised Merge Sort algorithm. It divides the list/array in two parts from mid using recursion, until only 1 element is left in each list. After that, it merges them both in another empty list and sorting them while doing so.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Merge Sort
data = [38, 27, 43, 3, 9, 82, 10]
def merge_sort(arr):
if len(arr) == 1 or len(arr) ==0:
return arr
mid = len(arr)//2
left_half = merge_sort(arr[:mid])
right_half = merge_sort(arr[mid:])
sorted_arr=[]
i=j=0
while i<len(left_half) and j<len(right_half):
if left_half[i]<right_half[j]:
sorted_arr.append(left_half[i])
i+=1
else:
sorted_arr.append(right_half[j])
j+=1
sorted_arr.extend(left_half[i:])
sorted_arr.extend(right_half[j:])
return sorted_arr
print(merge_sort(data))
Output:
1
[3, 9, 10, 27, 38, 43, 82]
This post is licensed under CC BY 4.0 by the author.