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
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/Sorting.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.7"
20 changes: 20 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file added read.txt
Empty file.
Binary file modified src/iterative_sorting/__pycache__/iterative_sorting.cpython-37.pyc
Binary file not shown.
108 changes: 94 additions & 14 deletions src/iterative_sorting/iterative_sorting.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,110 @@
# 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: Complete the selection_sort() function below

arr = [5, 4, 3, 1, 7, 2, 9]
print('arr in:', arr)
# Finds smallest then 2nd smallest and so on
# def selection_sort(arr):
# for i in range(len(arr) - 1):
# cur_index = i
# smallest_index = cur_index
# # print(smallest_index)
# for x in range(i, len(arr)):
# # print('x:::', x)
# if arr[smallest_index] > arr[x]:
# smallest_index = x
# # print(smallest_index)
# arr[i], arr[smallest_index] = arr[smallest_index], arr[i]
# # print('ordered arr:', arr)
# return arr

# selection_sort(arr)

# TO-DO: swap

# TO-DO: implement the Bubble Sort function below
# Compares 1st to 2nd, 2nd to third and so on
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for x in range(n-i-1):
if arr[x] > arr[x+1]:
arr[x], arr[x + 1] = arr[x + 1], arr[x]

# print('bubble arr:', arr)
return arr

bubble_sort(arr)


def bubble_sort2(arr):
print('arr in::', arr)
hasSwapped = True
while hasSwapped:
hasSwapped = False
for i in range(len(arr)):
if arr[i] > arr[i+1]:
arr[i], arr[i+1] = arr[i+1], arr[i]
# temp = arr[i]
# arr[i] = arr[i+1]
# arr[i+1] = temp
hasSwapped = True
print('bubble sort2:', arr)
return arr

bubble_sort2(arr)














import time
startTime = time.time()
endTime = time.time()
print(startTime, endTime)


















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

return arr


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

return arr
return arr



# print(smallest_index)
# # TO-DO: find next smallest element
# for x in range(0, len(arr) - 1):
# # (hint, can do in 3 loc)
# # TO-DO: swap
# print('x', x)
# if x > arr[0]:
# print('If i:', i)
# elif x < arr[0]:
# print('If x:', x)
#
112 changes: 112 additions & 0 deletions src/iterative_sorting/sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# TO-DO: Complete the selection_sort() function below

arr = [5, 4, 8, 3, 1, 7, 9, 1]
print('arr in:', arr)

# Finds smallest then 2nd smallest and so on
def selection_sort(arr):
for i in range(len(arr) - 1):
cur_index = i
smallest_index = cur_index
for j in range(i, len(arr)):
if arr[smallest_index] > arr[j]:
smallest_index = j
arr[i], arr[smallest_index] = arr[smallest_index], arr[i]
print('arr out:', arr)
return arr

# selection_sort(arr)


def selection_sort2(arr):
# 1 - loop one
for i in range(len(arr) - 1):
lowNum = i
# 2 - 2nd loop
for x in range(i, len(arr)):
# find lowest
if arr[lowNum] > arr[x]:
arr[i], arr[lowNum] = arr[lowNum], arr[i]

print('arr2 out:', arr)
return arr

# selection_sort2(arr)


# Compares 1st to 2nd, 2nd to third and so on
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for x in range(n-i-1):
if arr[x] > arr[x+1]:
arr[x], arr[x + 1] = arr[x + 1], arr[x]

# print('bubble arr:', arr)
return arr

# bubble_sort(arr)

def bubbleSort(arr):
n = len(arr)
for i in range(n):
for x in range(n-i-1):
if arr[x] > arr[x+1]:
arr[x], arr[x+1] = arr[x+1], arr[x]
print('arrbubble', arr)
return arr

# bubbleSort(arr)

def oneAtATime(arr):
x = len(arr)
for i in range(x):
for z in range(x-i-1):
if arr[z] > arr[z+1]:
arr[z], arr[z+1] = arr[z+1], arr[z]
print('1atatime', arr)
return arr

# oneAtATime(arr)

arrA = [1, 2, 3]
arrB = [4, 5, 6]

def merge(arrA, arrB):
elements = len(arrA) + len(arrB)
# print('elements:', elements)
merged_arr = [0] * elements
# UPER
# U - UNDERSTAND
# TAKE 2 ORDERED LISTS AND MERGE THEM TOGETHER
# P - PLAN
# FOR EACH ITEM

for i in arrA:
# print('i:', i)
merged_arr[i-1] = i
for x in arrB:
# print('x', x)
merged_arr[x - 1] = x

print('merged_arr:', merged_arr)
merged_arr.remove(1)
return merged_arr

merge(arrA, arrB)

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

# TO-DO: implement the Merge Sort function below USING RECURSION -- broken
# def merge_sort( arr ):
# if len(arr) > 1:
# middle = len(arr) // 2
# first_half = arr[0:middle]
# second_half = arr[middle:]
# left = merge_sort(first_half)
# right = merge_sort(second_half)
# arr = merge(left, right)
# print('merged sorted', arr)
# return arr
#
# merge_sort(arr)
11 changes: 9 additions & 2 deletions src/recursive_sorting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ While ***Quick Sort*** has "quick" in its name, it is typically not used as freq


## Merge Sort
Your boss asks you to organize an old filing cabinet with 20 years worth of financial documents. He would like things ordered chronologically. You feel overwhelemed. There are thousands of papers in this thing.
So you decide to break this insane task up into more manageable pieces. First, you focus on organizing a single drawer. But this still has a lot of pieces of data that need to be sorted. Within the drawer, you pull out a single folder. You lay out all the contents on a table, grabbing two pieces at a time, placing the older document on top of the newer one. Then, you start merging sorted sets of documents together until the folder is done. Once all the folders have been reassembled, you order the folders correctly in the drawer. And then you put sorted drawers back into the filing cabinet. This idea of breaking a large set of data down into small pieces, sorting the pieces, then merging them back together is what ***Merge Sort*** is all about.
Your boss asks you to organize an old filing cabinet with 20 years worth of financial documents.
He would like things ordered chronologically. You feel overwhelemed. There are thousands of papers in this thing.
So you decide to break this insane task up into more manageable pieces. First, you focus on organizing a single drawer.
But this still has a lot of pieces of data that need to be sorted. Within the drawer, you pull out a single folder.
You lay out all the contents on a table, grabbing two pieces at a time, placing the older document on top of the newer one.
Then, you start merging sorted sets of documents together until the folder is done. Once all the folders have been reassembled,
you order the folders correctly in the drawer. And then you put sorted drawers back into the filing cabinet.
This idea of breaking a large set of data down into small pieces, sorting the pieces, then merging them back together is
what ***Merge Sort*** is all about.

[(VIDEO) Merge-sort with Transylvanian-saxon (German) folk dance](https://www.youtube.com/watch?v=XaqR3G_NVoo)

Expand Down
Loading