-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathbubble_sort.py
More file actions
32 lines (22 loc) · 801 Bytes
/
bubble_sort.py
File metadata and controls
32 lines (22 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)