Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. class Count(object):
  2.     comparison_count = 0
  3.     swap_count = 0
  4.  
  5.     def __init__(self):
  6.         return
  7.  
  8.  
  9.  
  10. def quick_sort(a: list):
  11.     count = Count()
  12.  
  13.     def qsort(fst, lst, c):
  14.         if fst >= lst:
  15.             return
  16.  
  17.         i, j = fst, lst
  18.         border = a[random.randint(fst, lst)]
  19.         while i <= j:
  20.             while a[i] < border:
  21.                 i += 1
  22.             while a[j] > border:
  23.                 j -= 1
  24.             c.comparison_count += 1
  25.             if i <= j:
  26.                 c.swap_count += 1
  27.                 a[i], a[j] = a[j], a[i]
  28.                 i, j = i + 1, j - 1
  29.         qsort(fst, j, c)
  30.         qsort(i, lst, c)
  31.  
  32.     qsort(0, len(a) - 1, count)
  33.  
  34.     print("\n\nБЫСТРАЯ\n")
  35.     print(a)
  36.     print(
  37.         "Количество сравнений: " + str(count.comparison_count) + "\nКоличество перестановок: " + str(count.swap_count))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement