Repository navigation
Expand file tree
/
Copy pathrecursive_sorting.py
More file actions
71 lines (60 loc) · 1.97 KB
/
Copy pathrecursive_sorting.py
File metadata and controls
71 lines (60 loc) · 1.97 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
arr = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
print('arr in:', arr)
### STEP 1: Split our list into sub-lists until they are all length 1 or less
def merge_sort(arr):
# Check if it's length 1 or less. If so, return the list
if len(arr) <= 1:
return arr
# Divide in half
left = arr[ : len(arr) // 2]
print('left:', left)
right = arr[len(arr) // 2 : ]
print('right:', right)
# Sort the left
left = merge_sort(left)
print('left2ndtime:', left)
# Sort the right
right = merge_sort(right)
print('right2ndtime:', right)
# Merge together
return merge(left, right)
# STEP 2: Merge two sorted sublists and return the sorted merged list
def merge(arr_a, arr_b):
total_elements = len(arr_a) + len(arr_b)
merged_arr = [None] * total_elements
# Declare indices for each sublist
a = 0
b = 0
for i in range(total_elements):
# Check if either list is empty, if so append the other
if a >= len(arr_a):
merged_arr[i] = arr_b[b]
print('if: merged_arr[i]:', merged_arr[i])
b += 1
elif b >= len(arr_b):
merged_arr[i] = arr_a[a]
print('elif:1st- merged_arr[i]:', merged_arr[i])
a += 1
# Otherwise, compare and append the smallest of the two
elif arr_a[a] < arr_b[b]:
merged_arr[i] = arr_a[a]
print('elif: merged_arr[i]:', merged_arr[i])
a += 1
else:
merged_arr[i] = arr_b[b]
print('else: merged_arr[i]:', merged_arr[i])
b += 1
print('out:', merged_arr)
return merged_arr
merge_sort(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