Guest User

Untitled

a guest
Jul 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. try:
  4. raw_input # python 2
  5. except NameError:
  6. raw_input = input # python 3
  7.  
  8. def bubble_sort(arr):
  9. length = len(arr)
  10. for i in range(length):
  11. swapped = False
  12. for j in range(length - 1):
  13. if arr[j] > arr[j + 1]:
  14. swapped = True
  15. arr[j], arr[j + 1] = arr[j + 1], arr[j]
  16. if not swapped: break # Stop iteration if the arr is sorted
  17. return arr
  18.  
  19. if __name__ == '__main__':
  20. user_input = raw_input('Enter numbers separated by a comma: \n').strip()
  21. unsorted = [int(item) for item in user_input.split(',')]
  22. print(bubble_sort(unsorted))
Add Comment
Please, Sign In to add comment