Repository navigation
Expand file tree
/
Copy pathrecursive_sorting.py
More file actions
100 lines (82 loc) · 2.42 KB
/
Copy pathrecursive_sorting.py
File metadata and controls
100 lines (82 loc) · 2.42 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# TO-DO: complete the helper function below to merge 2 sorted arrays
def merge(arrA, arrB):
elements = len(arrA) + len(arrB)
merged_arr = [0] * elements
# TO-DO
i = 0
j = 0
k = 0
if len(arrA) < 1:
return arrB
if len(arrB) < 1:
return arrA
while i < len(arrA) and j < len(arrB):
if arrA[i] < arrB[j]:
print(arrA[i])
merged_arr[k] = arrA[i]
i += 1
k += 1
else:
merged_arr[k] = arrB[j]
j += 1
k += 1
while i < len(arrA):
merged_arr[k] = arrA[i]
i += 1
k += 1
while j < len(arrB):
merged_arr = arrB[j]
j += 1
k += 1
return merged_arr
# TO-DO: implement the Merge Sort function below USING RECURSION
def merge_sort(arr):
# TO-DO
# -1. Create condition to stop recursion when lists contain one element
if len(arr) > 1:
# -2. Find middle of list
mid = len(arr) // 2
# -3. Split list into two parts
left_list = arr[:mid] # This list is from beginning to mid
right_list = arr[mid:] # This list is from mid to end
# -4. Divide lists again using recursion
merge_sort(left_list)
merge_sort(right_list)
# -5. Merge lists
# Index for left list
i = 0
# Index for right list
j = 0
# Index for original list
k = 0
# -6. Create while loop for the case that lists contain a length of more than 1
while i < len(left_list) and j < len(right_list):
if left_list[i] < right_list[j]:
arr[k] = left_list[i]
i += 1
k += 1
else:
arr[k] = right_list[j]
j += 1
k += 1
# -7. Check if any elements of lists are skipped
while i < len(left_list):
arr[k] = left_list[i]
i += 1
k += 1
while j < len(right_list):
arr[k] = right_list[j]
j += 1
k += 1
return arr
# STRETCH: implement an in-place merge sort algorithm
def merge_in_place(arr, start, mid, end):
# TO-DO
return arr
def merge_sort_in_place(arr, l, r):
# TO-DO
return arr
# STRETCH: implement the Timsort function below
# hint: check out https://github.com/python/cpython/blob/master/Objects/listsort.txt
def timsort(arr):
return arr