Skip to content
Open
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
32 changes: 32 additions & 0 deletions Python/sorting/bubble_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def bubble_sort(arr):

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

# Initialize swapped to track if any swaps occur
swapped = False

# 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
arr[i], arr[i + 1] = arr[i + 1], arr[i]

# Mark that a swap has occurred
swapped = True

# If no swaps occurred, the list is already sorted
if not swapped:
break


# Sample list to be sorted
arr = [6,6,2]
print("Unsorted list is:")
print(arr)

bubble_sort(arr)

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