forked from SanjayDevTech/Code-with-love
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsethiojas_MergeSort.py
More file actions
72 lines (59 loc) · 1.95 KB
/
Copy pathsethiojas_MergeSort.py
File metadata and controls
72 lines (59 loc) · 1.95 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
68
69
70
71
72
from typing import List
def merge(array_one: List[int], array_two: List[int]) -> List[int]:
'''
Merge two sorted subarrays into a single sorted array
'''
pointer_one = pointer_two = 0
merged_array = []
# repeatedly choose the smallest element
# until one of the list gets exhausted
while (
pointer_one < len(array_one) and
pointer_two < len(array_two)
):
# choose the smallest element among the two
if array_one[pointer_one] < array_two[pointer_two]:
merged_array.append(array_one[pointer_one])
pointer_one += 1
else:
merged_array.append(array_two[pointer_two])
pointer_two += 1
# Insert the remaining elements if any
while pointer_one < len(array_one):
merged_array.append(array_one[pointer_one])
pointer_one += 1
while pointer_two < len(array_two):
merged_array.append(array_two[pointer_two])
pointer_two += 1
return merged_array
def merge_sort(array: List[int]) -> List[int]:
'''
Merge Sort function
'''
# base case
if len(array) <= 1:
# if there are less than or
# equal to 1 element in array
# return it
return array
mid = len(array) // 2
# divide array into left and right subarrays
# and sort them
left_half = merge_sort(array[ :mid])
right_half = merge_sort(array[mid:])
# merge and return the sorted arrays
return merge(left_half, right_half)
def generate_test_case(min_num: int, max_num: int, length: int) -> List[int]:
'''
Generate test cases for sorting
'''
from random import randint
test_case = [randint(min_num, max_num) for _ in range(length)]
print('Test case is')
print(test_case)
print('\n\n')
return test_case
if __name__ == '__main__':
test_case = generate_test_case(0, 1000, 100)
print('Sorted array')
print(merge_sort(test_case))