Skip to content

CSPT7 - Elisabeth Shah - Sorting - Assignment Submission #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc
Binary file not shown.
50 changes: 41 additions & 9 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,62 @@
import numpy as np


# TO-DO: Complete the selection_sort() function below
def selection_sort( arr ):
# loop through n-1 elements
for i in range(0, len(arr) - 1):
cur_index = i
smallest_index = cur_index

# TO-DO: find next smallest element
# (hint, can do in 3 loc)


for j in range(i + 1, len(arr)):
if arr[j] < arr[smallest_index]:
smallest_index = j

# TO-DO: swap



if cur_index != smallest_index:
arr[cur_index], arr[smallest_index] = \
arr[smallest_index], arr[cur_index]

return arr


# TO-DO: implement the Bubble Sort function below
def bubble_sort( arr ):

num_swaps = 1
while num_swaps != 0:
num_swaps = 0
for i in range(len(arr) - 1):
if arr[i] > arr[i + 1]:
arr[i], arr[i + 1] = arr[i + 1], arr[i]
num_swaps += 1
return arr


# STRETCH: implement the Count Sort function below
def count_sort( arr, maximum=-1 ):

return arr
#Find the maximum value:
if maximum == -1:
try:
maximum = max(arr)
except ValueError:
return []

# Store the count of each unique object.
count = np.zeros(maximum + 1)
for num in arr:
if num < 0:
return 'Error, negative numbers not allowed in Count Sort'
count[num] += 1

# Make the count array cumulative.
for i in range(1, len(count)):
count[i] += count[i - 1]

output_arr = arr.copy()
for num in arr:
output_arr[int(count[num]) - 1] = num
count[num] -= 1

return output_arr
22 changes: 11 additions & 11 deletions src/iterative_sorting/test_iterative.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest
import random
from iterative_sorting import *
from iterative_sorting import bubble_sort, count_sort, selection_sort

class IterativeSortingTest(unittest.TestCase):
def test_selection_sort(self):
Expand All @@ -26,16 +26,16 @@ def test_bubble_sort(self):
self.assertEqual(bubble_sort(arr4), sorted(arr4))

# Uncomment this test to test your count_sort implementation
# def test_counting_sort(self):
# arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
# arr2 = []
# arr3 = [1, 5, -2, 4, 3]
# arr4 = random.sample(range(200), 50)

# self.assertEqual(count_sort(arr1), [0,1,2,3,4,5,6,7,8,9])
# self.assertEqual(count_sort(arr2), [])
# self.assertEqual(count_sort(arr3), "Error, negative numbers not allowed in Count Sort")
# self.assertEqual(count_sort(arr4), sorted(arr4))
def test_counting_sort(self):
arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
arr2 = []
arr3 = [1, 5, -2, 4, 3]
arr4 = random.sample(range(200), 50)

self.assertEqual(count_sort(arr1), [0,1,2,3,4,5,6,7,8,9])
self.assertEqual(count_sort(arr2), [])
self.assertEqual(count_sort(arr3), "Error, negative numbers not allowed in Count Sort")
self.assertEqual(count_sort(arr4), sorted(arr4))


if __name__ == '__main__':
Expand Down
24 changes: 21 additions & 3 deletions src/recursive_sorting/recursive_sorting.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
# TO-DO: complete the helpe function below to merge 2 sorted arrays
# TO-DO: complete the helper function below to merge 2 sorted arrays
def merge( arrA, arrB ):
elements = len( arrA ) + len( arrB )
merged_arr = [0] * elements
# TO-DO

j, k = 0, 0
for i in range(len(merged_arr)):
if k > len(arrB) - 1:
merged_arr[i] = arrA[j]
j += 1
elif j > len(arrA) - 1:
merged_arr[i] = arrB[k]
k += 1
elif arrA[j] < arrB[k]:
merged_arr[i] = arrA[j]
j += 1
elif arrB[k] < arrA[j]:
merged_arr[i] = arrB[k]
k += 1
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:
arrA = arr[:len(arr) // 2]
arrB = arr[len(arr) // 2:]
return merge(merge_sort(arrA), merge_sort(arrB))
return arr


Expand Down
11 changes: 10 additions & 1 deletion src/recursive_sorting/test_recursive.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import unittest
import random
from recursive_sorting import *
from recursive_sorting import merge, merge_sort

class RecursiveSortingTests(unittest.TestCase):
def test_merge(self):
arr1 = [1, 3, 5, 7, 9]
arr2 = [2, 4, 6]
arr3 = []
arr4 = [1, 2, 3]
self.assertEqual(merge(arr1, arr2), [1, 2, 3, 4, 5, 6, 7, 9])
self.assertEqual(merge(arr3, arr4), [1, 2, 3])
self.assertEqual(merge(arr3, arr3), [])

def test_merge_sort(self):
arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
arr2 = []
Expand Down
48 changes: 31 additions & 17 deletions src/searching/searching.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
# STRETCH: implement Linear Search
def linear_search(arr, target):

# TO-DO: add missing code

return -1 # not found
for i in range(len(arr)):
if arr[i] == target:
return i
return -1 # not found


# STRETCH: write an iterative implementation of Binary Search
def binary_search(arr, target):

if len(arr) == 0:
return -1 # array empty

low = 0
high = len(arr)-1

# TO-DO: add missing code
if len(arr) == 0:
return -1 # array empty

low = 0
high = len(arr)

# TO-DO: add missing code
while high > low:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] > target:
high = mid
elif arr[mid] < target:
low = mid + 1


return -1 # not found
return -1 # not found


# STRETCH: write a recursive implementation of Binary Search
def binary_search_recursive(arr, target, low, high):

middle = (low+high)//2

if len(arr) == 0:
return -1 # array empty
# TO-DO: add missing if/else statements, recursive calls
mid = (low + high) // 2
if len(arr) == 0:
return -1 # array empty
# TO-DO: add missing if/else statements, recursive calls
if arr[mid] == target:
return mid
elif arr[mid] > target:
return binary_search_recursive(arr, target, low, mid)
else:
return binary_search_recursive(arr, target, mid + 1, high)
5 changes: 4 additions & 1 deletion src/searching/test_searching.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from searching import *
from searching import binary_search, binary_search_recursive, linear_search


class SearchingTests(unittest.TestCase):
Expand All @@ -16,8 +16,11 @@ def test_binary_search(self):
arr1 = [-9, -8, -6, -4, -3, -2, 0, 1, 2, 3, 5, 7, 8, 9]
arr2 = []

self.assertEqual(binary_search(arr1, -9), 0)
self.assertEqual(binary_search(arr1, -8), 1)
self.assertEqual(binary_search(arr1, 0), 6)
self.assertEqual(binary_search(arr1, 9), 13)

self.assertEqual(binary_search(arr2, 6), -1)
self.assertEqual(binary_search(arr2, 0), -1)

Expand Down