Skip to content

Commit cd6a002

Browse files
author
Abhishek-Jain-2
authored
Create BubbleSort.py
1 parent eb865a5 commit cd6a002

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

BubbleSort.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Python program for implementation of Bubble Sort
2+
3+
def bubbleSort(arr):
4+
n = len(arr)
5+
6+
# Traverse through all array elements
7+
for i in range(n-1):
8+
# range(n) also work but outer loop will repeat one time more than needed.
9+
10+
# Last i elements are already in place
11+
for j in range(0, n-i-1):
12+
13+
# traverse the array from 0 to n-i-1
14+
# Swap if the element found is greater
15+
# than the next element
16+
if arr[j] > arr[j+1] :
17+
arr[j], arr[j+1] = arr[j+1], arr[j]
18+
19+
# Driver code to test above
20+
arr = [64, 34, 25, 12, 22, 11, 90]
21+
22+
bubbleSort(arr)
23+
24+
print ("Sorted array is:")
25+
for i in range(len(arr)):
26+
print ("%d" %arr[i]),

0 commit comments

Comments
 (0)