Advertisement
SimeonTs

SUPyF Lists - Extra 04. Sort Array Using Bubble Sort

Jun 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.91 KB | None | 0 0
  1. """
  2. Lists
  3. Check your answer: https://judge.softuni.bg/Contests/Practice/Index/426#3
  4.  
  5. 04. Sort Array Using Bubble Sort
  6.  
  7. Problem:
  8. Read a list of integers on the first line of the console. After that, sort the list, using the Bubble Sort algorithm.
  9. Examples
  10. Input               Output
  11. 5 3 4 1 2           1 2 3 4 5
  12. 11 872 673 1 2      1 2 11 673 872
  13. 11 52 43 12 1 6     1 6 11 12 43 52
  14. """
  15.  
  16.  
  17. def short_bubble_sort(a_list):
  18.     exchanges = True
  19.     pass_num = len(a_list) - 1
  20.     while pass_num > 0 and exchanges:
  21.         exchanges = False
  22.         for i in range(pass_num):
  23.             if a_list[i] > a_list[i + 1]:
  24.                 exchanges = True
  25.                 temp = a_list[i]
  26.                 a_list[i] = a_list[i + 1]
  27.                 a_list[i + 1] = temp
  28.         pass_num = pass_num - 1
  29.  
  30.  
  31. nums = [int(item) for item in input().split(" ")]
  32. short_bubble_sort(nums)
  33.  
  34. for num in nums:
  35.     print(num, end=" ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement