fork download
  1. # insertion sort python program
  2.  
  3. def insertion_sort(array):
  4. for i in range(1, len(array)):
  5. j = i
  6. while j > 0 and array[j-1] > array[j]:
  7. array[j], array[j-1] = array[j-1], array[j]
  8. j = j - 1
  9. return array
  10.  
  11. array = [40, 30, 50, 10, 20]
  12. print("Array before the Insertion Sort :", array)
  13.  
  14. sorted_array = insertion_sort(array)
  15.  
  16. print("Array after the Insertion Sort :", sorted_array)
Success #stdin #stdout 0.04s 9724KB
stdin
Standard input is empty
stdout
Array before the Insertion Sort : [40, 30, 50, 10, 20]
Array after the Insertion Sort : [10, 20, 30, 40, 50]