Skip to content

Commit f9f5d40

Browse files
authored
Update bubble_sort.py
Added main method, Made it Python2 suitable, Enabled user input!
1 parent 356b514 commit f9f5d40

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

sorts/bubble_sort.py

+22-22
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
def bubbleSort(arr):
2-
n = len(arr)
1+
from __future__ import print_function
2+
3+
def bubble_sort(arr):
4+
n = len(arr)
5+
# Traverse through all array elements
6+
for i in range(n):
7+
# Last i elements are already in place
8+
for j in range(0, n-i-1):
9+
# traverse the array from 0 to n-i-1
10+
# Swap if the element found is greater
11+
# than the next element
12+
if arr[j] > arr[j+1] :
13+
arr[j], arr[j+1] = arr[j+1], arr[j]
14+
return arr
315

4-
# Traverse through all array elements
5-
for i in range(n):
6-
7-
# Last i elements are already in place
8-
for j in range(0, n-i-1):
9-
10-
# traverse the array from 0 to n-i-1
11-
# Swap if the element found is greater
12-
# than the next element
13-
if arr[j] > arr[j+1] :
14-
arr[j], arr[j+1] = arr[j+1], arr[j]
15-
16-
# Driver code to test above
17-
arr = [64, 34, 25, 12, 22, 11, 90]
18-
19-
bubbleSort(arr)
20-
21-
print ("Sorted array is:")
22-
for i in range(len(arr)):
23-
print ("%d" %arr[i]),
16+
if __name__ == '__main__':
17+
try:
18+
raw_input # Python 2
19+
except NameError:
20+
raw_input = input # Python 3
21+
user_input = raw_input('Enter numbers separated by a comma:').strip()
22+
unsorted = [int(item) for item in user_input.split(',')]
23+
print(*bubble_sort(unsorted), sep=',')

0 commit comments

Comments
 (0)