Skip to content

Commit

Permalink
Merge pull request #1 from mohamedfarhan-m/mohamedfarhan-m-patch-1
Browse files Browse the repository at this point in the history
Create bubsort.py
  • Loading branch information
mohamedfarhan-m authored Oct 12, 2024
2 parents 574ca15 + b205e18 commit ed668f8
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions python/bubsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def bubble_sort(arr):

# Outer loop to iterate through the list n times
for n in range(len(arr) - 1, 0, -1):

# Inner loop to compare adjacent elements
for i in range(n):
if arr[i] > arr[i + 1]:

# Swap elements if they are in the wrong order
swapped = True
arr[i], arr[i + 1] = arr[i + 1], arr[i]


# Sample list to be sorted
arr = [39, 12, 18, 85, 72, 10, 2, 18]
print("Unsorted list is:")
print(arr)

bubble_sort(arr)

print("Sorted list is:")
print(arr)

0 comments on commit ed668f8

Please sign in to comment.