Skip to content

Commit 8f4d745

Browse files
Create bubble.py
1 parent e9e7c96 commit 8f4d745

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

bubble.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
def bubble_sort(arr):
2+
n = len(arr)
3+
# Traverse through all array elements
4+
for i in range(n):
5+
# Last i elements are already sorted
6+
for j in range(0, n-i-1):
7+
# Traverse the array from 0 to n-i-1
8+
# Swap if the element found is greater
9+
# than the next element
10+
if arr[j] > arr[j+1]:
11+
arr[j], arr[j+1] = arr[j+1], arr[j]
12+
13+
# Example usage
14+
arr = [64, 34, 25, 12, 22, 11, 90]
15+
bubble_sort(arr)
16+
print("Sorted array is:", arr)

0 commit comments

Comments
 (0)