-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path09-sorting_improved.py
More file actions
83 lines (70 loc) · 3.3 KB
/
09-sorting_improved.py
File metadata and controls
83 lines (70 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# Улучшенная схема 09-sorting_common
# Украшения интерфейса, проверка корректности ввода,
# а также выбор пользователя между Bubble Sort и Quick Sort
# с помощью кнопок Radiobutton.
import tkinter as tk
import tkinter.messagebox as mb
mainw = tk.Tk()
mainw.title("BUBBLE SORT AND QUICK SORT")
mainw.minsize(width=275, height=185)
mainw.maxsize(width=275, height=185)
mainw.geometry("275x185")
sx = tk.Scrollbar(mainw, orient='horizontal')
sx.grid(row=6, column=1, sticky='ew')
sy = tk.Scrollbar(mainw, orient='vertical')
sy.grid(row=0, column=0, rowspan=6, sticky='ns')
box = tk.Listbox(mainw, xscrollcommand=sx.set, yscrollcommand=sy.set, background='peachpuff', foreground='red')
box.grid(row=0, column=1, rowspan=6)
sx.config(command=box.xview)
sy.config(command=box.yview)
entry = tk.Entry(mainw, background='lightskyblue', borderwidth=5)
entry.grid(row=0, column=2, sticky='nsew')
labelMode = tk.Label(mainw, text="Выберите алгоритм", background='dimgrey', foreground='lightskyblue', borderwidth=5)
labelMode.grid(row=1, column=2, sticky='nsew')
# Целочисленная переменная для Radiobutton'ов, сделаем так, что если
# пользователь выбрал Bubble Sort, она будет равна 0, если Quick Sort, то 1
mode = tk.IntVar()
radioBubble = tk.Radiobutton(mainw, text="Bubble Sort", variable=mode, value=0, background='peachpuff')
radioBubble.grid(row=2, column=2, sticky='nsew')
radioQuick = tk.Radiobutton(mainw, text="Quick Sort", variable=mode, value=1, background='peachpuff')
radioQuick.grid(row=3, column=2, sticky='nsew')
def append_item():
# Позволяем пользователю вводить только целые числа
try:
box.insert('end', int(entry.get()))
except ValueError:
mb.showerror("ОШИБКА", "Вводные данные должны быть целочисленного типа!")
entry.delete(0, 'end')
# Переписываем оба алгоритма
def bubble_sort(lst):
for i in range(len(lst)):
for j in range(len(lst) - 1):
if lst[j] > lst[j + 1]:
lst[j], lst[j + 1] = lst[j + 1], lst[j]
def quick_sort(lst):
if len(lst) > 1:
pivot = lst[0]
less = [i for i in lst if i < pivot]
equal = [i for i in lst if i == pivot]
greater = [i for i in lst if i > pivot]
return quick_sort(less) + equal + quick_sort(greater)
else:
return lst
def sort_box():
array = []
for item in box.get(0, 'end'):
array.append(int(item))
box.delete(0, 'end')
# Проверяем значение переменной Radiobutton'ов
# и применяем к списку соответствующий алгоритм
if mode.get() == 0:
bubble_sort(array)
else:
array = quick_sort(array)
for item in array:
box.insert('end', item)
buttonAdd = tk.Button(mainw, text="Добавить", command=append_item, background='lavenderblush')
buttonAdd.grid(row=4, column=2, sticky='nsew')
buttonSort = tk.Button(mainw, text="Сортировать", command=sort_box, background='lavenderblush')
buttonSort.grid(row=5, column=2, sticky='nsew')
mainw.mainloop()