Skip to content

Commit 9a76da4

Browse files
ElissaElissa
authored andcommitted
adding project folder
1 parent 08802ed commit 9a76da4

9 files changed

Lines changed: 175 additions & 0 deletions

File tree

‎.DS_Store‎

6 KB
Binary file not shown.

‎bubble.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# STRETCH: implement the Bubble Sort function below
2+
def bubble_sort( arr ):
3+
4+
return arr

‎insertion.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# TO-DO: implement the Insertion Sort function below
2+
def insertion_sort( arr ):
3+
4+
return arr

‎merge.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
### helper function
2+
def merge( arrA, arrB ):
3+
elements = len( arrA ) + len( arrB )
4+
merged_arr = [ ]
5+
a = 0
6+
b = 0
7+
# since arrA and arrB already sorted, we only need to compare the first element of each when merging!
8+
for i in range( 0, elements ):
9+
if a >= len(arrA): # all elements in arrA have been merged
10+
merged_arr.append(arrB[b])
11+
b += 1
12+
elif b >= len(arrB): # all elements in arrB have been merged
13+
merged_arr.append(arrA[a])
14+
a += 1
15+
elif arrA[a] < arrB[b]: # next element in arrA smaller, so add to final array
16+
merged_arr.append(arrA[a])
17+
a += 1
18+
else: # else, next element in arrB must be smaller, so add it to final array
19+
merged_arr.append(arrB[b])
20+
b += 1
21+
return merged_arr
22+
23+
24+
### recursive sorting function
25+
def merge_sort( arr ):
26+
if len( arr ) > 1:
27+
left = merge_sort( arr[ 0 : len( arr ) / 2 ] )
28+
right = merge_sort( arr[ len( arr ) / 2 : ] )
29+
arr = merge( left, right ) # merge() defined later
30+
return arr
31+
32+
33+
# STRETCH: implement an in-place merge sort algorithm
34+
def merge_sort_in_place(arr):
35+
# TO-DO
36+
37+
return arr

‎quick.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# TO-DO: implement the Quick Sort function below
2+
def quick_sort( arr ):
3+
4+
return arr

‎searching.py‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# implementation of Linear Search
2+
def linear_search(arr, target):
3+
for i in range( 0, len(arr) ):
4+
if arr[i] == target:
5+
return i
6+
return -1 # not found
7+
8+
9+
# ITERATIVE implementation of Binary Search
10+
def binary_search(arr, target):
11+
12+
if len(arr) == 0:
13+
return -1 # array empty
14+
15+
low = 0
16+
high = len(arr)-1
17+
18+
while low <= high:
19+
middle = (low+high)/2
20+
if target < arr[middle]:
21+
high = middle-1 # eliminate RHS
22+
elif target > arr[middle]:
23+
low = middle+1 # eliminate LHS
24+
else:
25+
return middle
26+
27+
return -1 # not found
28+
29+
30+
# RECURSIVE implementation of Binary Search
31+
def binary_search_recursive(arr, target, low, high):
32+
33+
middle = (low+high)/2
34+
35+
if len(arr) == 0:
36+
return -1 # array empty
37+
elif low > high:
38+
return -1 # not found
39+
elif arr[middle] == target:
40+
return middle
41+
else:
42+
if target < arr[middle]:
43+
high = middle-1 # eliminate RHS
44+
else:
45+
low = middle+1 # eliminate LHS
46+
return binary_search_recursive(arr, target, low, high)

‎selection.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Complete the selection_sort() function below in class with your instructor
2+
def selection_sort( arr ):
3+
# loop through n-1 elements
4+
for i in range(0, len(arr) - 1):
5+
cur_index = i
6+
smallest_index = cur_index
7+
# TO-DO: find next smallest element
8+
# (hint, can do in 3 loc)
9+
10+
11+
12+
13+
# TO-DO: swap
14+
15+
16+
17+
18+
return arr

‎tests.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import unittest
2+
from searching import *
3+
from selection import selection_sort
4+
from insertion import insertion_sort
5+
6+
class SortingTest(unittest.TestCase):
7+
8+
def test_linear_search(self):
9+
arr1 = [-2, 7, 3, -9, 5, 1, 0, 4, -6]
10+
arr2 = []
11+
12+
self.assertEqual(linear_search(arr1, 6), -1)
13+
self.assertEqual(linear_search(arr1, -6), 8)
14+
self.assertEqual(linear_search(arr1, 0), 6)
15+
self.assertEqual(linear_search(arr2, 3), -1)
16+
17+
18+
def test_binary_search(self):
19+
arr1 = [-9, -8, -6, -4, -3, -2, 0, 1, 2, 3, 5, 7, 8, 9]
20+
arr2 = []
21+
22+
self.assertEqual(binary_search(arr1, -8), 1)
23+
self.assertEqual(binary_search(arr1, 0), 6)
24+
self.assertEqual(binary_search(arr2, 6), -1)
25+
self.assertEqual(binary_search(arr2, 0), -1)
26+
27+
28+
def test_binary_search_recursive(self):
29+
arr1 = [-9, -8, -6, -4, -3, -2, 0, 1, 2, 3, 5, 7, 8, 9]
30+
arr2 = []
31+
32+
self.assertEqual(binary_search_recursive(arr1, -8, 0, len(arr1)-1), 1)
33+
self.assertEqual(binary_search_recursive(arr1, 0, 0, len(arr1)-1), 6)
34+
self.assertEqual(binary_search_recursive(arr2, 6, 0, len(arr1)-1), -1)
35+
self.assertEqual(binary_search_recursive(arr2, 0, 0, len(arr1)-1), -1)
36+
37+
38+
def test_selection(self):
39+
arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
40+
arr2 = []
41+
arr3 = [0, 1, 2, 3, 4, 5]
42+
43+
self.assertEqual(selection_sort(arr1), [0,1,2,3,4,5,6,7,8,9])
44+
self.assertEqual(selection_sort(arr2), [])
45+
self.assertEqual(selection_sort(arr3), [0,1,2,3,4,5])
46+
47+
48+
def test_insertion(self):
49+
arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
50+
arr2 = []
51+
arr3 = [0, 1, 2, 3, 4, 5]
52+
53+
self.assertEqual(insertion_sort(arr1), [0,1,2,3,4,5,6,7,8,9])
54+
self.assertEqual(insertion_sort(arr2), [])
55+
self.assertEqual(insertion_sort(arr3), [0,1,2,3,4,5])
56+
57+
if __name__ == '__main__':
58+
unittest.main()

‎timsort.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# STRETCH: implement the Timsort function below
2+
def timsort( arr ):
3+
4+
return arr

0 commit comments

Comments
 (0)