diff --git a/src/insertion_sort.py b/src/insertion_sort.py new file mode 100644 index 00000000..52f43667 --- /dev/null +++ b/src/insertion_sort.py @@ -0,0 +1,51 @@ + +def simple_implementation(arr): + # loop from 1 to end of array + for i in range(1, len(arr)): + temp = arr[i] + + j = i + + while j > 0 and temp < arr[j - 1]: + # copy element to left to this position + arr[j] = arr[j - 1] + + j -= 1 + + arr[j] = temp + + return arr + + +# Sorting books! + +class Book: + def __init__(self, author, title, genre): + self.author = author + self.title = title + self.genre = genre + + + def __str__(self): + return f'{self.title} by {self.author} in {self.genre}' + + + +books = [Book("Melville", "Moby Dick", "Whale stories"), Book("Immortal William", "Hamlet", "emo Danish princes")] + + +def insertion_sort(books): + for i in range(1, len(books)): + + temp = books[i] + + j = i + + # while we're not at front of list and these two should be swapped + while j > 0 and temp.genre < books[j - 1].genre: + books[j] = books[j - 1] + j -= 1 + + books[j] = temp + + return books \ No newline at end of file diff --git a/src/iterative_sorting/iterative_sorting.py b/src/iterative_sorting/iterative_sorting.py index e27496b3..a02d6944 100644 --- a/src/iterative_sorting/iterative_sorting.py +++ b/src/iterative_sorting/iterative_sorting.py @@ -1,30 +1,51 @@ -# TO-DO: Complete the selection_sort() function below +# 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) - - - + # (hint, can do in 3 loc) + for j in range(cur_index + 1, len(arr)): + if arr[j] < arr[smallest_index]: + smallest_index = j # TO-DO: swap - - - + 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 ): + for i in range(0, len(arr) - 1): + for j in range(0, len(arr) - 1 - i): + if arr[j] > arr[j + 1]: + arr[j], arr[j+1] = arr[j+1], arr[j] return arr # STRETCH: implement the Count Sort function below def count_sort( arr, maximum=-1 ): + if len(arr) == 0: + return arr - return arr \ No newline at end of file + if maximum == -1: + maximum = max(arr) + + counts = [0] * (maximum + 1) + + for value in arr: + if value < 0: + return "Error, negative numbers not allowed in Count Sort" + counts[value] += 1 + + j = 0 + for i in range(0, len(counts)): + while counts[i] > 0: + arr[j] = i + j += 1 + counts[i] -= 1 + + return arr diff --git a/src/iterative_sorting/test_iterative.py b/src/iterative_sorting/test_iterative.py index df11b648..468d92d7 100644 --- a/src/iterative_sorting/test_iterative.py +++ b/src/iterative_sorting/test_iterative.py @@ -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__': diff --git a/src/recursive_complexity.py b/src/recursive_complexity.py new file mode 100644 index 00000000..0efe3a8c --- /dev/null +++ b/src/recursive_complexity.py @@ -0,0 +1,28 @@ +total_times_count = 0 + +def count_down(n): + global total_times_count + total_times_count += 1 + + if n == 0: + return + + count_down(n - 1) + count_down(n - 1) + +def half(n): + if n <= 1: + return + + half(n / 2) + +# O(log n) + +# O(c^n) +# O(n) + +#(2^5) +# 2^10 + +count_down(5) +print(total_times_count) \ No newline at end of file diff --git a/src/recursive_sorting/recursive_sorting.py b/src/recursive_sorting/recursive_sorting.py index dcbf3757..970679af 100644 --- a/src/recursive_sorting/recursive_sorting.py +++ b/src/recursive_sorting/recursive_sorting.py @@ -3,25 +3,72 @@ def merge( arrA, arrB ): elements = len( arrA ) + len( arrB ) merged_arr = [0] * elements # TO-DO - + a = 0 + b = 0 + for i in range(0, elements): + if a >= len(arrA): + merged_arr[i] = arrB[b] + b += 1 + elif b >= len(arrB): + merged_arr[i] = arrA[a] + a += 1 + elif arrA[a] < arrB[b]: + merged_arr[i] = arrA[a] + a += 1 + else: + merged_arr[i] = arrB[b] + b += 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: + half = len(arr) // 2 + arrL = arr[half:] + arrR = arr[:half] + left = merge_sort(arrL) + right = merge_sort(arrR) + arr = merge(left, right) return arr - -# STRETCH: implement an in-place merge sort algorithm def merge_in_place(arr, start, mid, end): # TO-DO + i = mid + 1 + if arr[mid] <= arr[i]: + return arr + + while start <= mid and i <= end: + if arr[start] <= arr[i]: + start += 1 + else: + val = arr[i] + j = i + + while j != start: + arr[j] = arr[j - 1] + j -= 1 + + arr[start] = val + + start += 1 + mid += 1 + i += 1 return arr -def merge_sort_in_place(arr, l, r): +def merge_sort_in_place(arr, l, r): # TO-DO + if l < r: + m = l + (r - l) // 2 + merge_sort_in_place(arr, l, m) + merge_sort_in_place(arr, m + 1, r) + merge_in_place(arr, l, m, r) return arr diff --git a/src/recursive_sorting/test_recursive.py b/src/recursive_sorting/test_recursive.py index 12d62395..c437b011 100644 --- a/src/recursive_sorting/test_recursive.py +++ b/src/recursive_sorting/test_recursive.py @@ -17,19 +17,19 @@ def test_merge_sort(self): self.assertEqual(merge_sort(arr5), sorted(arr5)) # Uncomment this test to test your in-place merge sort implementation - # def test_in_place_merge_sort(self): - # arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] - # arr2 = [] - # arr3 = [2] - # arr4 = [0, 1, 2, 3, 4, 5] - # arr5 = random.sample(range(200), 50) + def test_in_place_merge_sort(self): + arr1 = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7] + arr2 = [] + arr3 = [2] + arr4 = [0, 1, 2, 3, 4, 5] + arr5 = random.sample(range(200), 50) - # self.assertEqual(merge_sort_in_place(arr1, 0, len(arr1)-1), [0,1,2,3,4,5,6,7,8,9]) - # self.assertEqual(merge_sort_in_place(arr2, 0, len(arr2)-1), []) - # self.assertEqual(merge_sort_in_place(arr3, 0, len(arr3)-1), [2]) - # self.assertEqual(merge_sort_in_place(arr4, 0, len(arr4)-1), [0,1,2,3,4,5]) - # self.assertEqual(merge_sort_in_place(arr5, 0, len(arr5)-1), sorted(arr5)) + self.assertEqual(merge_sort_in_place(arr1, 0, len(arr1)-1), [0,1,2,3,4,5,6,7,8,9]) + self.assertEqual(merge_sort_in_place(arr2, 0, len(arr2)-1), []) + self.assertEqual(merge_sort_in_place(arr3, 0, len(arr3)-1), [2]) + self.assertEqual(merge_sort_in_place(arr4, 0, len(arr4)-1), [0,1,2,3,4,5]) + self.assertEqual(merge_sort_in_place(arr5, 0, len(arr5)-1), sorted(arr5)) if __name__ == '__main__': - unittest.main() \ No newline at end of file + unittest.main() diff --git a/src/searching/searching.py b/src/searching/searching.py index d4969a0c..4985882a 100644 --- a/src/searching/searching.py +++ b/src/searching/searching.py @@ -1,30 +1,39 @@ -# STRETCH: implement Linear Search def linear_search(arr, target): - - # TO-DO: add missing code + for i in range(0, len(arr)): + if arr[i] == target: + return i - return -1 # not found + 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 + if len(arr) == 0: + return -1 # array empty - # TO-DO: add missing code + low = 0 + high = len(arr)-1 - return -1 # not found + while low <= high: + middle = (low + high) // 2 + if target < arr[middle]: + high = middle - 1 + elif target > arr[middle]: + low = middle + 1 + else: + return middle + 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 + middle = (low+high)//2 + + if len(arr) == 0: + return -1 # array empty + # TO-DO: add missing if/else statements, recursive calls + if arr[middle] == target: + return middle + elif arr[middle] > target: + return binary_search_recursive(arr, target, low, middle - 1) + else: + return binary_search_recursive(arr, target, middle + 1, high) diff --git a/src/space_complexity.py b/src/space_complexity.py new file mode 100644 index 00000000..d835cd48 --- /dev/null +++ b/src/space_complexity.py @@ -0,0 +1,42 @@ +n = [None] * 1000 + +# O(1) +def simple_function(n): + return n + +simple_function(n) + +# O(n) +def make_another_array(n): + inner_array = [] + + for i in n: + inner_array.append(i) + + return inner_array + +make_another_array(n) + +# O(n^2) +def make_matrix(n): + matrix = [] + + for item in n: + row = [] + + for i in n: + + row.append(i * 2) + + matrix.append(row) + + +def factorial(n): + + if n == 0: + return 1 + + return n * factorial(n - 1) + +factorial(3) +# 3 * 2 * 1 * 1 \ No newline at end of file