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
Binary file not shown.
38 changes: 18 additions & 20 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
# 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



for low in range(0, len(arr)):
min_dex = low
for i in range(low+ 1, len(arr)):
if(arr[min_dex] > arr[i]):
min_dex = i

arr[low], arr[min_dex] = arr[min_dex], arr[low]
return arr


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

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


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

return arr
return arr
65 changes: 59 additions & 6 deletions src/recursive_sorting/recursive_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,69 @@
def merge( arrA, arrB ):
elements = len( arrA ) + len( arrB )
merged_arr = [0] * elements
# TO-DO


return merged_arr


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

if len(arr)>1:
left = arr[:len(arr)//2]
right = arr[len(arr)//2:]

#recursion
merge_sort(left)
merge_sort(right)

i=0
j=0
k=0

while i < len(left) and j < len(right):
if left[i] < right[j]:
arr[k]=left[i]
i=i+1
else:
arr[k]=right[j]
j=j+1
k=k+1

while i < len(left):
arr[k]=left[i]
i=i+1
k=k+1

while j < len(right):
arr[k]=right[j]
j=j+1
k=k+1

return arr
























return arr


# STRETCH: implement an in-place merge sort algorithm
Expand All @@ -20,7 +73,7 @@ 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
Expand Down