Skip to content

Commit

Permalink
Merge pull request #459 from realhunter7869/python-algos
Browse files Browse the repository at this point in the history
#258 Python algos
  • Loading branch information
hhhrrrttt222111 authored Oct 1, 2021
2 parents 31c0314 + 4ca23c0 commit bc1f06f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ALGORITHMS/Sorting/Bubble Sort/bubbleSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def bubbleSort(arr):
n = len(arr)
ctr = 0
for i in range(n-1):
for j in range(0, n-i-1):
if arr[j] > arr[j + 1] :
arr[j], arr[j + 1] = arr[j + 1], arr[j]
ctr += 1
print(ctr)


lst = [1, 2, 3, 5, 4]
bubbleSort(lst)
# since lists are mutable
# the main list itself changes
print(lst)
18 changes: 18 additions & 0 deletions ALGORITHMS/Sorting/Bubble Sort/modifiedBubbleSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def bubbleSort(arr):
n = len(arr)
ctr = 0
for i in range(n):
swapped = False
for j in range(0, n-i-1):
if arr[j] > arr[j+1] :
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
if swapped == False:
break


lst = [1, 2, 3, 5, 4]
bubbleSort(lst)
# since lists are mutable
# the main list itself changes
print(lst)
15 changes: 15 additions & 0 deletions ALGORITHMS/Sorting/Insertion Sort/insertionSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
def insertionSort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j] :
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key


lst = [1, 2, 3, 5, 4]
insertionSort(lst)
# since lists are mutable
# the main list itself changes
print(lst)

0 comments on commit bc1f06f

Please sign in to comment.