Advertisement
SimeonTs

SUPyF Lists - Extra 05. Sort Array Using Insertion Sort

Jun 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. """
  2. Lists
  3. Check your answer: https://judge.softuni.bg/Contests/Practice/Index/426#4
  4.  
  5. 05. Sort Array Using Insertion Sort
  6.  
  7. Problem:
  8. Read a list of integers on the first line of the console. After that, sort the list, using the Insertion 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 insertion_sort(arr):
  18.     for i in range(1, len(arr)):
  19.         key = arr[i]
  20.         j = i - 1
  21.         while j >= 0 and key < arr[j]:
  22.             arr[j + 1] = arr[j]
  23.             j -= 1
  24.         arr[j + 1] = key
  25.  
  26.  
  27. nums = [int(item) for item in input().split(" ")]
  28. insertion_sort(nums)
  29.  
  30. for num in nums:
  31.     print(num, end=" ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement