mhrabbi

Insertion Sort.py

Jun 26th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1.  #Insertion Sort Function
  2.  #R@1313I
  3.  
  4. def InsertionSort(arr):
  5.      for i in range( 1,len(arr)):
  6.          key = arr[i]
  7.          j = i-1
  8.  
  9.          #Iteration / Change to  < for increasing sorted order
  10.          while j >= 0 and arr[j] > key:
  11.              arr[j+1] = arr[j]  #
  12.              j=j-1
  13.  
  14.          arr[j+1] = key
  15.  
  16.  
  17. #Checking Function
  18. arr = [3,2,5,9,1,0]
  19.  
  20. InsertionSort(arr)
  21.  
  22. print("Sorted Number is: ")
  23.  
  24. for i in range(len(arr)):
  25.     print(arr[i])
Add Comment
Please, Sign In to add comment