-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_Sort_Algorithmn.py
More file actions
52 lines (42 loc) · 1.66 KB
/
Merge_Sort_Algorithmn.py
File metadata and controls
52 lines (42 loc) · 1.66 KB
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
# Merge Sort Algorithmn
# This is a divide and conquer algorithm that divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves.
def merge_sort(array):
if len(array) <= 1:
return
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
# Example usage
if __name__ == '__main__': # This condition ensures that the code inside this block will only be executed if the script is run directly, and not when it is imported as a module in another script.
numbers = [4, 10, 6, 14, 2, 1, 8, 5]
print('Unsorted array: ')
print(numbers)
merge_sort(numbers)
print('Sorted array: ')
print(numbers)
## Output:
# Unsorted array:
# [4, 10, 6, 14, 2, 1, 8, 5]
# Sorted array:
# [1, 2, 4, 5, 6, 8, 10, 14]