giGii

f3_B_problem1

Mar 27th, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.88 KB | None | 0 0
  1. def comparator(item_1, item_2):
  2.     # это чтобы числа сравнивать (за числами проще следить, чем за кортежами)
  3.     if isinstance(item_1, int):
  4.         return item_1 > item_2
  5.     # если подан кортеж то это уже компаратор должен поработать
  6.     if (item_1[0] == item_2[0]) and (item_1[1] == item_2[1]):
  7.         return item_1 < item_2
  8.     else:
  9.         return item_1 > item_2
  10.  
  11.  
  12. def quick_sort(arr, l_bound=None, r_bound=None, switches=None):
  13.     l_bound = 0 if l_bound is None else l_bound
  14.     r_bound = len(arr) - 1 if r_bound is None else r_bound
  15.  
  16.     middle = l_bound + (r_bound - l_bound) // 2
  17.     pivot = arr[middle]
  18.  
  19.     left = l_bound
  20.     right = r_bound
  21.  
  22.     if r_bound - l_bound <= 1:
  23.         if comparator(arr[r_bound], arr[l_bound]):
  24.             arr[l_bound], arr[r_bound] = arr[r_bound], arr[l_bound]
  25.         return
  26.  
  27.     while right > left:
  28.     # while switches != 0:
  29.     #     switches = 0
  30.         while comparator(arr[left], pivot) and left < right:
  31.             left += 1
  32.         while comparator(pivot, arr[right]) and left < right:
  33.             right -= 1
  34.         # flag = True if middle in (left, right) else False
  35.         if comparator(arr[right], arr[left]):
  36.             arr[left], arr[right] = arr[right], arr[left]
  37.             # switches += 1
  38.             left += 1
  39.             right -= 1
  40.             # pivot = arr[middle]
  41.         # if switches != 0:
  42.         #     left = l_bound
  43.         #     right = r_bound
  44.  
  45.  
  46.     quick_sort(arr, l_bound, middle)
  47.     quick_sort(arr, middle, r_bound)
  48.  
  49. # ввод для тестов на сортировку чисел
  50. seq = list(map(int, input().split()))
  51. quick_sort(seq)
  52. print('моя работа:        ', *seq)
  53. print('стандартная работа:', *sorted(seq, reverse=True))
  54.  
  55. """
  56. почему не сортируются?
  57. 2 3 13 7 4 18 9 16 10 17 12 5 1 20 6 19 11 14 15 8 → вывод: 20 19 18 17 16 13 10 9 7 15 12 14 11 8 6 5 4 3 2 1
  58. 11 15 12 6 1 17 7 10 8 18 4 20 14 3 2 5 19 9 16 13 → вывод: 20 19 18 12 17 10 8 7 6 16 15 14 13 4 11 5 9 3 2 1
  59. 8 18 6 20 4 19 17 3 15 16 11 7 14 9 12 5 10 1 13 2 → вывод: 20 19 18 17 16 15 8 6 4 14 13 12 11 9 10 7 5 3 2 1
  60. 19 14 5 6 10 20 7 8 4 13 12 16 17 15 18 9 11 1 2 3 → вывод: 20 19 18 17 16 15 14 13 8 12 11 10 9 7 6 5 4 3 2 1
  61. """
  62.  
  63. """
  64. ввод для задачи
  65. (раскомментировать, чтобы потестировать вводы по условию; ввод для тестов на сортировку чисел при этом закомментировать)
  66. n = int(input())
  67. participants = []
  68. for participant in range(n):
  69.    line = input().split()
  70.    participants.append((int(line[1]), -int(line[2]), line[0]))
  71.  
  72. quick_sort(participants)
  73. for participant in participants:
  74.    print(participant[2])
  75. """
  76.  
Advertisement
Add Comment
Please, Sign In to add comment