8000 Create bubble_sort.py · AllAlgorithms/python@f5d18af · GitHub
[go: up one dir, main page]

Skip to content

Commit f5d18af

Browse files
authored
Create bubble_sort.py
0 parents  commit f5d18af

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

sorting/bubble_sort.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Python implementation of bubble sort
2+
#
3+
# Author: Carlos Abraham Hernandez
4+
5+
def bubbleSort(arr):
6+
n = len(arr)
7+
8+
for i in range(n):
9+
for j in range(0, n-i-1):
10+
if arr[j] > arr[j+1] :
11+
arr[j], arr[j+1] = arr[j+1], arr[j]
12+
13+
def printArray(arr):
14+
for i in range(len(arr)):
15+
print ("%d" %arr[i]),
16+
17+
# Test
18+
arr = [46, 24, 33, 10, 2, 81, 50]
19+
print ("Unsorted Array:")
20+
printArray(arr)
21+
print ('\n')
22+
23+
bubbleSort(arr)
24+
25+
print ("Sorted Array:")
26+
printArray(arr)

0 commit comments

Comments
 (0)
0