-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge-sort.py
More file actions
67 lines (55 loc) · 2.63 KB
/
merge-sort.py
File metadata and controls
67 lines (55 loc) · 2.63 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# This is an implementation of the merge sort algorithm in Python,
# which is a divide-and-conquer algorithm that splits a given array into two halves,
# sorts them, and then merges them together
# This script is well docummented with comments to explain every step of this proccess
def merge_sort(array):
# Returns 1 if the array contains none or just a single number
if len(array) <= 1:
return
# Calculate the middle index of the array and split it in two halves
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
# Recursive call to the merge_sort function
merge_sort(left_part)
merge_sort(right_part)
# These variables will be used to iterate through the halves and merge them together
left_array_index = 0
right_array_index = 0
sorted_index = 0
# Iterates through both halves and compares their elements
while left_array_index < len(left_part) and right_array_index < len(right_part):
# If the left element is smaller, it is placed in the sorted position in the original array,
# and the left_array_index is incremented
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
# If the right element is smaller, it is placed in the sorted position in the original array,
# and the right_array_index is incremented
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
# The sorted_index is incremented after placing an element in the sorted position
# For the left half:
# Place the remaining elements in the sorted position in the original array
# and increment the indices
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
# For the right half:
# Place the remaining elements in the sorted position in the original array
# and increment the indices
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
# Checks if the script is being run directly or imported as a module
if __name__ == '__main__':
# Define an unsorted array and print it
numbers = [4, 10, 6, 14, 2, 1, 8, 5]
print('Unsorted array:',numbers)
# Call the merge_sort function on the unsorted array and print the sorted array
merge_sort(numbers)
print('Sorted array:',str(numbers))