Advertisement
AlfonsoPEREZ

insertion sort time complexity

Apr 27th, 2020
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. import time, random
  2.  
  3. def insertion_sort(lst):
  4.     length = range(1, len(lst))
  5.     for i in length:
  6.         value = lst[i]
  7.  
  8.         while lst[i-1] > value and i > 0:
  9.             lst[i], lst[i - 1] = lst[i - 1], lst[i]
  10.             i = i - 1
  11.  
  12.     return(lst)
  13.  
  14. def random_element_listt():
  15.     global listt
  16.  
  17.     #NUMBERS FROM 0-999
  18.     #PICKS RANDOM NUMBER AND PUTS IT IN LISTT
  19.     for i in range(1000):
  20.         listt.append(random.choice(range(1, 1000)))
  21.  
  22. listt = []
  23.  
  24. def main():
  25.     random_element_listt()
  26.  
  27.     print()
  28.     print("INSERTION SORT")
  29.     print()
  30.     t1 = time.time()
  31.     print(insertion_sort(listt))
  32.     t2 = time.time()
  33.     print('elapsed time=', t2 - t1)
  34.     print('Big O Notation: O(n^2)')
  35.  
  36. main()
  37. #MADE BY AVMP
  38. #PLEASE CHECK OUT MY CHANNEL https://www.youtube.com/channel/UCQor7IURWM-lGT-tmFbFSCw
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement