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
27 changes: 27 additions & 0 deletions src/insertion_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
def insertion_sort(items):
# Split the list into sorted and unsorted
# For each element in unsorted...
counter = 0
for i in range(1, len(items)):
# Insert that element into the correct place in sorted
# Store the elements in a temp variable
temp = items[i]
# Shifting all larger sorted elements to the right by 1
j = i
while j > 0 and temp < items[j - 1]:
print('**********************')
counter += 1
print(items)
items[j] = items[j - 1]
j -= 1
print(items)
# Insert the element after the first smaller element
items[j] = temp
print(items)
print(f'Counter = {counter}')
return items


l = [7, 4, 9, 2, 6, 3, 0, 8, 5, 1]

insertion_sort(l)
41 changes: 41 additions & 0 deletions src/iterative_binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Assume names is sorted
# Assume names is a list
def binary_search(to_find, names):
# Cut our list in half, examine the midpoint item
start = 0
end = len(names)
while end - start > 0:
mid = start + (end - start) // 2
item = names[mid]
print(start)
print(mid)
print(end)
print('***')
# If the item is equal to to_find:
if item == to_find:
return True
# Otherwise, if it's smaller
elif item > to_find:
end = mid - 1
# Repeat binary search on first half of the list
# Otherwise
else:
start = mid + 1
# Repeat binary search on second half
return False


names = ['Jack', 'Jill', 'Joe', 'James', 'Jessica', 'Jones', 'Jeremy', 'Jamie']


def sorting(list):
list.sort()
return list


names = sorting(names)

print(names)
to_find = ['Jamie']

print(binary_search('Jamie', names))
Binary file modified src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc
Binary file not shown.
86 changes: 86 additions & 0 deletions src/iterative_sorting/asdf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def bubble_sort(arr):
# Create temporary items 1 and 2 (one per compared item)
# If item 2 is smaller than item 1, item 2 is swapped to the left of item 1
for i in range(len(arr)):
# print(arr[9])
j = i
# print('********************')
# print(f'i @ beginning = {i}')
# print(f'j @ beginning = {j}')
for z in range(len(arr)):
j = z
# print(f'J ({j}) = Z ({z})')
# print(f'Z = ({z})')
while j < (len(arr) - 1):
left = arr[j]
# print(arr)
# print(j)
right = arr[j + 1]
# print(f'while loop #{j}')
# print(f'Pre-Swap: temp-Left = ({left}); temp-Right = ({right})')
# print(f'Pre-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})')
for m in range(len(arr)):
if (left > right) and ((j + 1) < (len(arr) - 1)):
# print('if left > right: swap')
# print(f'if statement #{j}')
arr[j] = right
arr[j+1] = left
j += 1
left = arr[j]
# print(left)
# print(arr[j])
# print(arr[j+1])
right = arr[j+1]
# print(arr)
# print('^Swapped array print ^')
# print(f'Post-Swap: temp-Left = ({left}); temp-Right = ({right})')
# print(f'Post-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})')
if (left > right) and ((j + 1) == (len(arr) - 1)):
arr[j] = right
arr[j + 1] = left
left = arr[j]
right = arr[j + 1]
# print(arr)
# print(left)
# print(right)
# print(f'j = {j}')
# print('WOW')
j += 1
else:
# print('break')
break
if (right > left) and ((j + 1) < (len(arr) - 1)):
# print('if right > left: move on to next')
# print(f'if statement #{j}')
j += 1
left = arr[j]
right = arr[j+1]
# print(arr)
# print('^Same array print ^')
# print(f'No-Swap: temp-Left = ({left}); temp-Right = ({right})')
# print(f'No-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})')
if right == left:
# print('Right == Left')
pass
else:
# print('Else statement')
break
break
# print(f'i @ end = {i}')
# print(f'j @ end = {j}')
return arr

print(bubble_sort(arr))

import random
l = list(range(10000))
random.shuffle(l)
l_copy = l.copy()

import time
start_time = time.time()
print(bubble_sort(l_copy))
end_time = time.time()
print(f'runtime: {end_time - start_time}')
171 changes: 156 additions & 15 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,171 @@
# TO-DO: Complete the selection_sort() function below
def selection_sort( arr ):
# Selection sort: first item in array checks second item to see if first item is larger or not. If first item is larger,
# first item and second item switch positions in array. Then the recently switched item checks itself in the same manner
# against all the other numbers until switched and then that switched number does the same.



# # # 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
# # return arr
#


arr = [7, 4, 9, 2, 6, 3, 0, 8, 5, 1]

def selection_sort(arr):
# loop through n-1 elements
for i in range(0, len(arr) - 1):
print('****************************\n')
cur_index = i
smallest_index = cur_index
# TO-DO: find next smallest element
# (hint, can do in 3 loc)




# TO-DO: swap
print(f'First for loop current index {cur_index}')
print(f'First for loop smallest index {smallest_index}')
for j in range((i+1), len(arr)):
print('*****SECOND FOR LOOP*****')
smallest_index_element = arr[smallest_index]
compared_item = arr[j]
print(f'Second for loop current j index {j}')
print(f'Smallest index element {smallest_index_element}')
print(f'Second for loop compared index element {compared_item}')
if smallest_index_element > compared_item:
print('*****IF STATEMENT*****')
small = arr[j]
larger = arr[smallest_index]
print(f'If statement Small = {small}; Larger = {larger}')
arr[smallest_index] = small
arr[j] = larger
print(f'If statement Array = {arr}')
print(f'j at end of if statement = {j}')


return arr

print(selection_sort(arr))

# NO PRINT STATEMENT VERSION
def bubble_sort(arr):
# Create temporary items 1 and 2 (one per compared item)
# If item 2 is smaller than item 1, item 2 is swapped to the left of item 1
for i in range(len(arr)):
for z in range(len(arr)):
while z < (len(arr) - 1):
left = arr[z]
right = arr[z + 1]
for m in range(len(arr)):
if (left > right) and ((z + 1) < (len(arr) - 1)):
arr[z] = right
arr[z+1] = left
z += 1
left = arr[z]
right = arr[z+1]
if (left > right) and ((z + 1) == (len(arr) - 1)):
arr[z] = right
arr[z + 1] = left
left = arr[z]
right = arr[z + 1]
z += 1
else:
break
if (right > left) and ((z + 1) < (len(arr) - 1)):
z += 1
left = arr[z]
right = arr[z+1]
if right == left:
pass
else:
break
break
return arr

print(bubble_sort(arr))


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

return arr

# def bubble_sort(arr):
# # Create temporary items 1 and 2 (one per compared item)
# # If item 2 is smaller than item 1, item 2 is swapped to the left of item 1
# for i in range(len(arr)):
# # print(arr[9])
# # j = i
# # print('********************')
# # print(f'i @ beginning = {i}')
# # print(f'j @ beginning = {j}')
# for z in range(len(arr)):
# # j = z
# # print(f'J ({j}) = Z ({z})')
# # print(f'Z = ({z})')
# while z < (len(arr) - 1):
# left = arr[z]
# # print(arr)
# # print(j)
# right = arr[z + 1]
# # print(f'while loop #{j}')
# # print(f'Pre-Swap: temp-Left = ({left}); temp-Right = ({right})')
# # print(f'Pre-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})')
# for m in range(len(arr)):
# if (left > right) and ((z + 1) < (len(arr) - 1)):
# # print('if left > right: swap')
# # print(f'if statement #{j}')
# arr[z] = right
# arr[z+1] = left
# z += 1
# left = arr[z]
# # print(left)
# # print(arr[j])
# # print(arr[j+1])
# right = arr[z+1]
# # print(arr)
# # print('^Swapped array print ^')
# # print(f'Post-Swap: temp-Left = ({left}); temp-Right = ({right})')
# # print(f'Post-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})')
# if (left > right) and ((z + 1) == (len(arr) - 1)):
# arr[z] = right
# arr[z + 1] = left
# left = arr[z]
# right = arr[z + 1]
# # print(arr)
# # print(left)
# # print(right)
# # print(f'j = {j}')
# # print('WOW')
# z += 1
# else:
# # print('break')
# break
# if (right > left) and ((z + 1) < (len(arr) - 1)):
# # print('if right > left: move on to next')
# # print(f'if statement #{j}')
# z += 1
# left = arr[z]
# right = arr[z+1]
# # print(arr)
# # print('^Same array print ^')
# # print(f'No-Swap: temp-Left = ({left}); temp-Right = ({right})')
# # print(f'No-Swap: array-Left = ({arr[j]}); array-Right = ({arr[j + 1]})')
# if right == left:
# # print('Right == Left')
# pass
# else:
# # print('Else statement')
# break
# break
# # print(f'i @ end = {i}')
# # print(f'j @ end = {j}')
# return arr
#
# print(bubble_sort(arr))

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

return arr
38 changes: 38 additions & 0 deletions src/recursive_binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Assume names is sorted
# Assume names is a list
def binary_search(to_find, names):
# If the list is empty
if len(names) == 0:
return False
# Cut our list in half, examine the midpoint item
mid = len(names) // 2
item = names[mid]
# If the item is equal to to_find:
print(item)
print(names)
if item == to_find:
return True
# Otherwise, if it's smaller
elif item > to_find:
return binary_search(to_find, names[:mid])
# Repeat binary search on first half of the list
# Otherwise
else:
return binary_search(to_find, names[mid+1:])
# Repeat binary search on second half


names = ['Jack', 'Jill', 'Joe', 'James', 'Jessica', 'Jones', 'Jeremy', 'Jamie']


def sorting(list):
list.sort()
return list


names = sorting(names)

print(names)
to_find = ['Jamie']

print(binary_search('Jamie', names))