Skip to content

Commit c180cdf

Browse files
ElissaElissa
authored andcommitted
Updating recursive_sorting solutions, tests
1 parent 4edbde7 commit c180cdf

3 files changed

Lines changed: 117 additions & 10 deletions

File tree

‎.DS_Store‎

0 Bytes
Binary file not shown.

‎project/recursive_sorting.py‎

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
### helper function
22
def merge( arrA, arrB ):
33
elements = len( arrA ) + len( arrB )
4-
merged_arr = [ ]
4+
merged_arr = [0] * elements
55
a = 0
66
b = 0
77
# since arrA and arrB already sorted, we only need to compare the first element of each when merging!
88
for i in range( 0, elements ):
99
if a >= len(arrA): # all elements in arrA have been merged
10-
merged_arr.append(arrB[b])
10+
merged_arr[i] = arrB[b]
1111
b += 1
1212
elif b >= len(arrB): # all elements in arrB have been merged
13-
merged_arr.append(arrA[a])
13+
merged_arr[i] = arrA[a]
1414
a += 1
1515
elif arrA[a] < arrB[b]: # next element in arrA smaller, so add to final array
16-
merged_arr.append(arrA[a])
16+
merged_arr[i] = arrA[a]
1717
a += 1
1818
else: # else, next element in arrB must be smaller, so add it to final array
19-
merged_arr.append(arrB[b])
19+
merged_arr[i] = arrB[b]
2020
b += 1
2121
return merged_arr
2222

@@ -31,19 +31,91 @@ def merge_sort( arr ):
3131

3232

3333
# STRETCH: implement an in-place merge sort algorithm
34-
def merge_sort_in_place(arr):
34+
# based on C solution at: https://www.geeksforgeeks.org/in-place-merge-sort/
35+
def merge_in_place(arr, start, mid, end):
3536
# TO-DO
37+
start2 = mid + 1
38+
39+
# If the direct merge is already sorted
40+
if (arr[mid] <= arr[start2]):
41+
return arr
42+
43+
# Two pointers to maintain start
44+
# of both arrays to merge
45+
while start <= mid and start2 <= end:
46+
47+
# If element 1 is in right place
48+
if arr[start] <= arr[start2]:
49+
start += 1
50+
51+
else:
52+
value = arr[start2]
53+
index = start2
54+
55+
# Shift all the elements between element 1
56+
# element 2, right by 1.
57+
while index != start:
58+
arr[index] = arr[index - 1]
59+
index -= 1
60+
61+
arr[start] = value
62+
63+
# Update all the pointers
64+
start += 1
65+
mid += 1
66+
start2 += 1
67+
68+
return arr
69+
70+
71+
# l is for left index and r is right index of the
72+
# sub-array of arr to be sorted
73+
def merge_sort_in_place(arr, l, r):
74+
if l < r:
75+
76+
# Same as (l + r) / 2, but avoids overflow
77+
# for large l and r
78+
m = l + (r - l) / 2
79+
80+
# Sort first and second halves
81+
merge_sort_in_place(arr, l, m)
82+
merge_sort_in_place(arr, m + 1, r)
83+
84+
merge_in_place(arr, l, m, r)
3685

3786
return arr
3887

3988

4089
# TO-DO: implement the Quick Sort function below
41-
def quick_sort( arr ):
90+
def quick_sort( arr, low, high ):
91+
# base case
92+
if high-low <= 0:
93+
return arr
94+
95+
# partition
96+
pivot = low
97+
for i in range(low+1, high+1):
98+
if arr[i] < arr[pivot]:
99+
# move to LHS of pivot - 2 swaps
100+
temp = arr[i]
101+
arr[i] = arr[pivot+1]
102+
arr[pivot+1] = temp
103+
104+
temp = arr[pivot]
105+
arr[pivot] = arr[pivot+1]
106+
arr[pivot+1] = temp
107+
pivot += 1
108+
109+
# Quick Sort LHS, RHS
110+
arr = quick_sort(arr, low, pivot-1)
111+
arr = quick_sort(arr, pivot+1, high)
42112

43113
return arr
44114

45115

46-
# TO-DO: implement the Timsort function below
116+
# STRETCH: implement the Timsort function below
117+
# hint: check out https://github.com/python/cpython/blob/master/Objects/listsort.txt
47118
def timsort( arr ):
48119

120+
# see source code at: https://github.com/python/cpython/blob/master/Objects/listobject.c
49121
return arr

‎project/tests.py‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22
from searching import *
3-
from iterative_sorting import *
4-
from recursive_sorting import *
3+
from iterative_sorting import *
4+
from recursive_sorting import merge_sort, merge_sort_in_place, quick_sort
55

66
class SortingTest(unittest.TestCase):
77

@@ -74,5 +74,40 @@ def test_count(self):
7474
self.assertEqual(count_sort(arr2), [])
7575
self.assertEqual(count_sort(arr3), "Error, negative numbers not allowed in Count Sort")
7676

77+
78+
def test_merge(self):
79+
arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
80+
arr2 = []
81+
arr3 = [2]
82+
arr4 = [0, 1, 2, 3, 4, 5]
83+
84+
self.assertEqual(merge_sort(arr1), [0,1,2,3,4,5,6,7,8,9])
85+
self.assertEqual(merge_sort(arr2), [])
86+
self.assertEqual(merge_sort(arr3), [2])
87+
self.assertEqual(merge_sort(arr4), [0,1,2,3,4,5])
88+
89+
90+
def test_merge_in_place(self):
91+
arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
92+
arr2 = []
93+
arr3 = [2]
94+
arr4 = [0, 1, 2, 3, 4, 5]
95+
96+
self.assertEqual(merge_sort_in_place(arr1, 0, len(arr1)-1), [0,1,2,3,4,5,6,7,8,9])
97+
self.assertEqual(merge_sort_in_place(arr2, 0, len(arr2)-1), [])
98+
self.assertEqual(merge_sort_in_place(arr3, 0, len(arr3)-1), [2])
99+
self.assertEqual(merge_sort_in_place(arr4, 0, len(arr4)-1), [0,1,2,3,4,5])
100+
101+
102+
def test_quick(self):
103+
arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
104+
arr2 = []
105+
arr3 = [0, 1, 2, 3, 4, 5]
106+
107+
self.assertEqual(quick_sort(arr1, 0, len(arr1)-1), [0,1,2,3,4,5,6,7,8,9])
108+
self.assertEqual(quick_sort(arr2, 0, len(arr2)-1), [])
109+
self.assertEqual(quick_sort(arr3, 0, len(arr3)-1), [0,1,2,3,4,5])
110+
111+
77112
if __name__ == '__main__':
78113
unittest.main()

0 commit comments

Comments
 (0)