Keksike

лаба по МПИ2

Sep 26th, 2021 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. from random import randint
  2. import time
  3.  
  4. def sozdanie(a):
  5.     file_test = open("file.txt", "w")
  6.     for i in range(a):
  7.         number = randint(0,10000)
  8.         file_test.write(str(number) + '\n')
  9.     file_test.close()
  10.     return 0
  11.  
  12. def vstavka(list):  #Сортировка методов вставок
  13.     p = 0
  14.     for i in range(len(list)):
  15.         j = i-1
  16.         key = list[i]
  17.         while list[j] > key and j >= 0:
  18.             list[j+1] = list[j]
  19.             j -= 1
  20.         list[j+1] = key
  21.         if (i%100) == 0:
  22.             p+=1
  23.             print(p, "%")
  24.     return(list)
  25.  
  26. def chtenie(a):
  27.     with open("file.txt") as f:
  28.         for line in f:
  29.             a.append([int(x) for x in line.split()])
  30.     return(a)
  31.  
  32. sozdanie(10000)
  33.  
  34. data = []
  35.  
  36. start_chtenie = time.time()
  37. chtenie(data)
  38. stop_chtenie = time.time()
  39. print("prochital")
  40.  
  41. start_sortirovka = time.time()
  42. vstavka(data)
  43. stop_sortirovka = time.time()
  44. print("otsortiroval")
  45.  
  46. print((stop_chtenie-start_chtenie)*1000)
  47. print((stop_sortirovka-start_sortirovka)*1000)
  48.  
  49.  
Add Comment
Please, Sign In to add comment