Skip to content

Commit

Permalink
hhhrrrttt222111#258 bubble sort and modified bubble sort in python
Browse files Browse the repository at this point in the history
  • Loading branch information
realhunter7869 committed Oct 1, 2021
1 parent ec40036 commit 224ed7d
Show file tree
Hide file tree
Showing 2 changed files with 34 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)

0 comments on commit 224ed7d

Please sign in to comment.