daily pastebin goal
28%
SHARE
TWEET

Untitled

a guest Feb 22nd, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from random import randint, choice
  2. from time import time
  3.  
  4.  
  5. def swap(m, n):
  6.     tmp = a[m]
  7.     a[m] = a[n]
  8.     a[n] = tmp
  9.  
  10.  
  11. def bubble():
  12.     r = len(a)
  13.     was_swap = True
  14.     while (r > 1) and was_swap:
  15.         was_swap = False
  16.         for j in range(0, r - 1):
  17.             if a[j] > a[j + 1]:
  18.                 swap(j, j + 1)
  19.                 was_swap = True
  20.         r -= 1
  21.  
  22.  
  23. def shell():
  24.     n = len(a)
  25.     d = n // 2
  26.     while d > 0:
  27.         for i in range(0, n - d):
  28.             j = i
  29.             while (j >= 0) and (a[j] > a[j + d]):
  30.                 swap(j, j + d)
  31.             j -= 1
  32.         d -= 1
  33.  
  34.  
  35. def quick(l, r):
  36.     if l >= r: return
  37.     else:
  38.         bar = choice(a[l: r + 1])
  39.         i = l
  40.         j = r
  41.         while i <= j:
  42.             while a[i] < bar: i += 1
  43.             while a[j] > bar: j -= 1
  44.             if i <= j:
  45.                 swap(i, j)
  46.                 i += 1
  47.                 j -= 1
  48.         quick(l, j)
  49.         quick(i, r)
  50.  
  51.  
  52. def info():
  53.     t0 = time()
  54.     bubble()
  55.     t1 = time()
  56.     values.append(round(t1 - t0, 5))
  57.     t0 = time()
  58.     shell()
  59.     t1 = time()
  60.     values.append(round(t1 - t0, 5))
  61.     t0 = time()
  62.     quick(0, len(a) - 1)
  63.     t1 = time()
  64.     values.append(round(t1 - t0, 5))
  65.  
  66.  
  67. def show(v):
  68.     k = 0
  69.     print('Метод    Отсортированная    Случайная    Обратная')
  70.     for i in range(3):
  71.         print('Метод №', i + 1, '\t\t', v[i], '\t\t', v[i + 3], '\t\t', v[i + 6])
  72.  
  73.  
  74. n = int(input("Введите количество элементов в последовательности: "))
  75. values = []
  76. a = [i for i in range(n)]
  77. info()
  78. del a
  79. a = [randint(0, 101) for i in range(n)]
  80. info()
  81. del a
  82. a = [i for i in range(n - 1, -1, -1)]
  83. info()
  84. del a
  85. show(values)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top