Repository navigation
Expand file tree
/
Copy pathrecursive_sorting.py
More file actions
71 lines (50 loc) · 1.43 KB
/
Copy pathrecursive_sorting.py
File metadata and controls
71 lines (50 loc) · 1.43 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
import math
# TO-DO: complete the helpe function below to merge 2 sorted arrays
def merge( arrA, arrB ):
merged_arr = []
# TO-DO
while arrA and arrB:
if arrA[0] < arrB[0]:
merged_arr.append(arrA[0])
arrA.remove(arrA[0])
else:
merged_arr.append(arrB[0])
arrB.remove(arrB[0])
while arrA:
merged_arr.append(arrA[0])
arrA.remove(arrA[0])
while arrB:
merged_arr.append(arrB[0])
arrB.remove(arrB[0])
return merged_arr
# TO-DO: implement the Merge Sort function below USING RECURSION
def merge_sort( arr ):
# TO-DO
l = len(arr)
if l <= 1:
return arr
arrayOne = [n for n in arr[:math.floor(l/2)]]
arrayTwo = [n for n in arr[math.floor(l/2):]]
# print(arrayOne)
# print(arrayTwo)
arrayOne = merge_sort(arrayOne)
arrayTwo = merge_sort(arrayTwo)
# print(arrayOne)
# print(arrayTwo)
arr = merge(arrayOne, arrayTwo)
# print(arr)
return arr
# a = [4,9,3,2,7,5,8,1,6,0]
# x = merge_sort(a)
# print(x)
# 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