Skip to content
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
48 changes: 30 additions & 18 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
# 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)




# TO-DO: swap




cur_index = i#establishes first index
for j in range(i+1, len(arr)):#the array besides the first
if arr[cur_index] > arr[j]:# if the first is greater than anything
cur_index = j#new starting point
arr[i], arr[cur_index] = arr[cur_index], arr[i]
return arr


list = [1, 5, 8, 4, 2, 9, 6, 0, 3, 7]
# TO-DO: implement the Bubble Sort function below
def bubble_sort( arr ):

swapped = True
while swapped:
swapped = False
for i in range(len(arr)-1):
if arr[i] > arr[i+1]:
arr[i],arr[i+1] = arr[i+1],arr[i]
swapped = True
return arr




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

return arr
# first is to tally all of the occurances of each number and make an array that is ordered by index of all numbers 0-9
# second is to add the tally number to the next number in the line
count_index = [0,0,0,0,0,0,0,0,0,0]
for i in range(len(arr)):#tally occurances
count_index[arr[i]] += 1
for i in range(len(count_index)):#add the previous number to the next number
count_index[i] = count_index[i]+count_index[i-1]
count_index.pop()
count_index.insert(0,0)#shift all numbers to the right
output_arr = [0]*len(arr)
for i in range(len(arr)):#populate output array
output_arr[count_index[arr[i]]] = arr[i]
count_index[arr[i]]+=1
return output_arr
7 changes: 7 additions & 0 deletions src/recursive_sorting/rec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

def greet(n):
if n == 1:
return 1
return n * greet(n - 1)


60 changes: 45 additions & 15 deletions src/recursive_sorting/recursive_sorting.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
# TO-DO: complete the helpe function below to merge 2 sorted arrays

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

return merged_arr
result = []
i = j = 0
while i < len(arrA) and j < len(arrB):
if arrA[i] < arrB[j]:
result.append(arrA[i])
i+=1
else:
result.append(arrB[j])
j+=1
result += arrA[i:]
result += arrB[j:]


return result

#arrA = [2,3,4,5,6,7]
#arrB = [9,10,11,12,13]
#merge(arrA, arrB)

# TO-DO: implement the Merge Sort function below USING RECURSION
def merge_sort( arr ):
# TO-DO
#(base case) if the array is empty or lenght 1 return
if len(arr) <= 1:
return arr
left = merge_sort(arr[:len(arr)//2])
right = merge_sort(arr[len(arr)//2:])
arr = merge(left, right)

return arr
arr = [2,5,3,6,9,4,15,0]
merge_sort(arr)
# TO-DO

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

#split here

#find the middle of arr
#put stuff to the left in left
#put the stuff to the right in right

#merge left and right

# STRETCH: implement an in-place merge sort algorithm
def merge_in_place(arr, start, mid, end):
# # STRETCH: implement an in-place merge sort algorithm
#def merge_in_place(arr, start, mid, end):
# TO-DO

return arr
# return arr

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

return arr
#return arr


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

return arr
#return arr
2 changes: 1 addition & 1 deletion src/recursive_sorting/test_recursive.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ def test_merge_sort(self):


if __name__ == '__main__':
unittest.main()
unittest.main()