Repository navigation
Expand file tree
/
Copy pathrecursive_sorting.py
More file actions
120 lines (99 loc) · 3.27 KB
/
Copy pathrecursive_sorting.py
File metadata and controls
120 lines (99 loc) · 3.27 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# TO-DO: complete the helpe function below to merge 2 sorted arrays
def merge(arrA, arrB):
elements = len(arrA) + len(arrB)
merged_arr = [0] * elements
# TO-DO
if len(arrA) == 0:
return arrB
elif len(arrB) == 0:
return arrA
index_left = index_right = 0
list_merged = []
while len(list_merged) < elements:
if arrA[index_left] <= arrB[index_right]:
list_merged.append(arrA[index_left])
index_left += 1
else:
list_merged.append(arrB[index_right])
index_right += 1
if index_right == len(arrB):
list_merged += arrA[index_left:]
break
elif index_left == len(arrA):
list_merged += arrB[index_right:]
break
merged_arr = list_merged
return merged_arr
# TO-DO: implement the Merge Sort function below USING RECURSION
def merge_sort(arr):
# TO-DO
if len(arr) <= 1:
return arr
else:
median = len(arr)//2
left = arr[:median]
right = arr[median: len(arr)]
return merge(merge_sort(left), merge_sort(right))
# STRETCH: implement an in-place merge sort algorithm
"""
Approach:
Maintain two pointers which point to start of the segments which have to be merged.
Compare the elements at which the pointers are present.
If element1 < element2 then element1 is at right position, simply increase pointer1.
Else place element2 in its right position and all the elements
at the right of element2 will be shifted right by one position.
Increment all the pointers by 1.
"""
# Merges two subarrays of arr[].
# First subarray is arr[l..m]
# Second subarray is arr[m+1..r]
# Inplace Implementation
def merge_in_place(arr, start, mid, end):
# TO-DO
# get a second start point from mid-point
start2 = mid + 1
# If the adjacent items is already sorted return
if arr[mid] < arr[start2]:
return
# Two pointers to maintain start
# of both arrays to merge
while start <= mid and start2 <= end:
# element 1 = arr[start]
# element 2 = arr[start2]
# If element 1 is in right place incerement index of element 1
# else swap
if arr[start] < arr[start2]:
start += 1
else:
value = arr[start2]
index = start2
# Shift all the elements between element 1
# element 2, right by 1. (Swap)
while index != start:
arr[index] = arr[index - 1]
index -= 1
arr[start] = value
# Update all the pointers
start += 1
mid += 1
start2 += 1
return arr
"""
l is for left index and r is right index of the
sub-array of arr to be sorted
"""
def merge_sort_in_place(arr, l, r):
# TO-DO
if l < r:
# Same as (l + r) / 2, but avoids overflow
# for large l and r
median = int(l + (r - l)/2)
#Sort first and second halves
merge_sort_in_place(arr, l, median)
merge_sort_in_place(arr, median + 1, r)
merge_in_place(arr, l, median, r)
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