uccjshrimpton

Ugly Insertion Sort

May 22nd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.78 KB | None | 0 0
  1. def insertion_sort(unsorted_list):
  2.    
  3.     sorted_list = []
  4.     while len(unsorted_list) >= 1:
  5.         if len(sorted_list) == 0:
  6.             sorted_list.insert(0,unsorted_list[0])
  7.             del unsorted_list[0]
  8.  
  9.         else:
  10.             counter = 0
  11.             inserted = False
  12.             while inserted == False:
  13.                 if len(sorted_list) > counter:
  14.                     if unsorted_list[0] < sorted_list[counter]:
  15.                         sorted_list.insert(counter,unsorted_list[0])
  16.                         inserted = True
  17.                     counter += 1
  18.                 else:
  19.                     sorted_list.append(unsorted_list[0])
  20.                     inserted = True
  21.                    
  22.             del unsorted_list[0]
  23.        
  24.     return sorted_list
Advertisement
Add Comment
Please, Sign In to add comment