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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,8 @@ $RECYCLE.BIN/
*.lnk

# End of https://www.gitignore.io/api/windows,macos,linux,python

/book.py
/book_data.csv
/intro.py
/sort.py
11 changes: 11 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]

[requires]
python_version = "3.8"
44 changes: 36 additions & 8 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,58 @@
# TO-DO: Complete the selection_sort() function below
# Complete the selection_sort() function below

def selection_sort( arr ):
# loop through n-1 elements
# last element by the time we reach it is already sorted
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)


# find next smallest element

# using a while loop
# j = cur_index
# while j < len(arr):
# # same steps as for loop
# j += 1

# TO-DO: swap
for j in range(cur_index, len(arr)):
if arr[j] < arr[smallest_index]:
#found new smallest thing
smallest_index = j

# swap
arr[cur_index], arr[smallest_index] = arr[smallest_index], arr[cur_index]

return arr


return arr


# TO-DO: implement the Bubble Sort function below
# implement the Bubble Sort function below

def bubble_sort( arr ):
# swap_flag = true
swapped = True

# while a swap_flag = true
while swapped is True:
# swap_flag resets to false
swapped = False

# for each item in arr
for i in range(len(arr) - 1):
left = i
right = i + 1
# compare to item on RHS
if arr[left] > arr[right]:
# swap if LHS > RHS and set swap_flag = true
arr[left], arr[right] = arr[right], arr[left]
swapped = True

return arr


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

return arr
return arr
35 changes: 35 additions & 0 deletions src/iterative_sorting/smallest_value_index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# look at the value at index i numbers[i]

# if this value is smaller than the value at smallest value idx
# smallest_value_index = i
# else
# continue to the next index umber
# increment i += 1

#go back tho the top of the loop
#


def smallest_value_index(numbers):
smallest_value_idx = 0

for i in range(len(numbers)):
if numbers[i] < numbers[smallest_value_idx]:
smallest_value_idx = i

i += 1

return smallest_value_idx


# numbers = [5, 1, 8, 4, 2, 9, 6, 0, 3, 7]
# print(smallest_value_index(numbers))
#
# numbers1 = [6, 1, 28, 14, 2, 9, 6, 30, 8]
# print(smallest_value_index(numbers1))
#
# numbers2 = [6, 28, 14, 2, 9, 6, 2, 30, 8]
# print(smallest_value_index(numbers2))
#
# numbers3 = [6, 28, -14, 2, 9, 6, 2, 30, 8]
# print(smallest_value_index(numbers3))
26 changes: 26 additions & 0 deletions src/iterative_sorting/subarrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from smallest_value_index import smallest_value_index

numbers = [5, 1, 8, 4, 2, 9, 6, 0, 3, 7]
print("numbers", numbers)

left = [numbers[0]]
print("left:", left)

right = numbers[0 + 1:-1]
print("right:", right)

smallest_idx = smallest_value_index(right)
print("smallest_idx:", smallest_idx)

left_last = left[-1]
print("left_last:", left_last)

if right[smallest_idx] < left_last:

print("right[smallest_idx]:", right[smallest_idx])
print("left[-1]", left[-1])
# swap
right[smallest_idx] , left[-1] = left[-1], right[smallest_idx]


print("numbers", numbers)