Skip to content

Ethan Hoover #268

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 2 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
29 changes: 16 additions & 13 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
# TO-DO: Complete the selection_sort() function below
def selection_sort( arr ):
# 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(i+1, len(arr)):
if arr[j] < arr[smallest_index]:
smallest_index = j

# TO-DO: swap



temp = arr[cur_index]
arr[cur_index] = arr[smallest_index]
arr[smallest_index] = temp

return arr


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

def bubble_sort(arr):
for i in range(0, len(arr) - 1):
for j in range(i+1, len(arr)):
if arr[i] > arr[j]:
arr[i], arr[j] = arr[j], arr[i]
return arr


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

return arr
return arr
46 changes: 40 additions & 6 deletions src/recursive_sorting/recursive_sorting.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
# TO-DO: complete the helpe function below to merge 2 sorted arrays
def merge( arrA, arrB ):
elements = len( arrA ) + len( arrB )
import math


def merge(arrA, arrB):
elements = len(arrA) + len(arrB)
merged_arr = [0] * elements
# TO-DO

i = 0
j = 0
merged_index = 0
while i < len(arrA) and j < len(arrB):
if arrA[i] > arrB[j]:
merged_arr[merged_index] = arrB[j]
j += 1

elif arrA[i] < arrB[j]:
merged_arr[merged_index] = arrA[i]
i += 1

merged_index += 1

merged_arr = merged_arr[:merged_index]
if i < len(arrA):
merged_arr.extend(arrA[i:])
if j < len(arrB):
merged_arr.extend(arrB[j:])

return merged_arr


# TO-DO: implement the Merge Sort function below USING RECURSION
def merge_sort( arr ):


def merge_sort(arr):
# TO-DO
if len(arr) <= 1:
return arr
mid_index = math.floor(len(arr)/2)
left = arr[:mid_index]
right = arr[mid_index:]

left_sorted = merge_sort(left)
right_sorted = merge_sort(right)

arr = merge(left_sorted, right_sorted)
return arr


Expand All @@ -20,14 +53,15 @@ def merge_in_place(arr, start, mid, end):

return arr

def merge_sort_in_place(arr, l, r):

def merge_sort_in_place(arr, l, r):
# TO-DO

return arr


# STRETCH: implement the Timsort function below
# hint: check out https://github.com/python/cpython/blob/master/Objects/listsort.txt
def timsort( arr ):
def timsort(arr):

return arr
54 changes: 37 additions & 17 deletions src/searching/searching.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
# STRETCH: implement Linear Search
# STRETCH: implement Linear Search
def linear_search(arr, target):

# TO-DO: add missing code
for i in range(len(arr)):
if arr[i] == target:
return i

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


# STRETCH: write an iterative implementation of Binary Search
# 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

# TO-DO: add missing code
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
return -1 # not found


# STRETCH: write a recursive implementation of Binary Search
# 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
while low <= high:
if arr[middle] == target:
return middle
elif target < arr[middle]:
high = middle-1
else:
low = middle+1
return binary_search_recursive(arr, target, low, high)
return -1