Advertisement
artyomblack

insertionSort (simple)

Apr 6th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.35 KB | None | 0 0
  1. def insertionSort (arr):
  2.     for i in range (1, len(arr)):
  3.         leftIndex = i - 1
  4.         key = arr[i]
  5.         while leftIndex >= 0 and key < arr[leftIndex]:
  6.             arr[leftIndex + 1] = arr[leftIndex]
  7.             leftIndex -= 1
  8.         arr[leftIndex + 1] = key
  9.     return arr
  10.            
  11. # Example
  12. print(insertionSort([5, 3, 6, 2, 10]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement