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 modified src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc
Binary file not shown.
25 changes: 17 additions & 8 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
def selection_sort( arr ):
# loop through n-1 elements
for i in range(0, len(arr) - 1):
cur_index = i
cur_index = i #
smallest_index = cur_index
# TO-DO: find next smallest element
# TO-DO: find next smallest element, (x, y = y, x)
# (hint, can do in 3 loc)


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


# TO-DO: swap
temp = arr[smallest_index]
arr[smallest_index] = arr[cur_index]
arr[cur_index] = temp



Expand All @@ -19,12 +23,17 @@ def selection_sort( arr ):


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

def bubble_sort(arr):
n = len(arr)
for i in range(n - 1):
# For each element in the array...
for j in range(n - 1 - i):
# Check it's neighbor to the right...
if arr[j] > arr[j + 1]:
# If neighbor is smaller, swap and make Flag true
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 ):

return arr
41 changes: 40 additions & 1 deletion src/recursive_sorting/recursive_sorting.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
# TO-DO: complete the helpe function below to merge 2 sorted arrays
# We are only comparing the first elements of the arrays, as they are already sorted
def merge( arrA, arrB ):
elements = len( arrA ) + len( arrB )
merged_arr = [0] * elements
# TO-DO

# Initialise pointers for the front of Arrays A & B
a = 0
b = 0
for i in range(0, elements):
# compare thearrays
if a >= len(arrA): # all elements in arrA have been merged
merged_arr[i] = arrB[b]
b += 1
elif b >= len(arrB): # all elements in arrB have been merged
merged_arr[i] = arrA[a]
a += 1
elif arrA[a] < arrB[b]: # next element in arrA smaller, so add to final array
merged_arr[i] = arrA[a]
a += 1
else: # else, next element in arrB must be smaller, so add it to final array
merged_arr[i] = arrB[b]
b += 1
return merged_arr


def merge(arrA, arrB):
elements = len(arrA) + len(arrB)
merged_arr = [0] * elements
a = 0
b = 0
# since arrA and arrB already sorted, we only need to compare the first element of each when merging!
for i in range(0, elements):



# TO-DO: implement the Merge Sort function below USING RECURSION
def merge_sort( arr ):
# TO-DO
def merge_sort( arr ):
if len(arr) > 1:
# split the array
mid = len(arr) // 2
# split again - left
left = arr[:mid]
# split again - right
right = arr[mid:]
arr = merge(left, right)

mergeSort(left)
mergeSort(right)

return arr

Expand Down