moved all Python sorting algorithms into 1 folder

This commit is contained in:
Joe James
2018-06-29 16:07:43 -07:00
committed by GitHub
parent fe3e849350
commit c78e569ceb
6 changed files with 556 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
#---------------------------------------
# Bubble Sort
#---------------------------------------
# not optimized
def bubble_sort1(A):
for i in range (0, len(A) - 1):
for j in range (0, len(A) - i - 1):
if A[j] > A[j+1]:
A[j], A[j+1] = A[j+1], A[j]
# optimized to exit if no swaps occur
def bubble_sort2(A):
for i in range (0, len(A) - 1):
done = True
for j in range (0, len(A) - i - 1):
if A[j] > A[j+1]:
A[j], A[j+1] = A[j+1], A[j]
done = False
if done:
return
A = [5,9,1,2,4,8,6,3,7]
print(A)
bubble_sort1(A)
print(A)