Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- try:
- raw_input # python 2
- except NameError:
- raw_input = input # python 3
- def bubble_sort(arr):
- length = len(arr)
- for i in range(length):
- swapped = False
- for j in range(length - 1):
- if arr[j] > arr[j + 1]:
- swapped = True
- arr[j], arr[j + 1] = arr[j + 1], arr[j]
- if not swapped: break # Stop iteration if the arr is sorted
- return arr
- if __name__ == '__main__':
- user_input = raw_input('Enter numbers separated by a comma: \n').strip()
- unsorted = [int(item) for item in user_input.split(',')]
- print(bubble_sort(unsorted))
Add Comment
Please, Sign In to add comment