-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmerge_sort.py
More file actions
34 lines (28 loc) · 715 Bytes
/
merge_sort.py
File metadata and controls
34 lines (28 loc) · 715 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
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
left = merge_sort(left)
right = merge_sort(right)
return merge(left, right)
def merge(left, right):
result = []
i = 0
j = 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result += left[i:]
result += right[j:]
return result
# Example Usage
arr = [3, 6, 1, 9, 2, 8, 5, 7, 4]
sorted_arr = merge_sort(arr)
print("Original Array: ", arr)
print("Sorted Array: ", sorted_arr)