Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import *
- from time import perf_counter
- from tkinter import *
- from tkinter import messagebox
- import tkinter.ttk as tk
- import matplotlib.pyplot as plt
- from numpy import *
- from math import *
- def before_sort_func():
- pass
- def after_sort_func():
- pass
- def sort_example():
- data = []
- before = ''
- for i in range(9):
- number = randint(-100, 100)
- data.append(number)
- before += str(number) + ' '
- window = Tk()
- window.title('Пример работы сортировки')
- window.geometry('300x120')
- before_sort_label = Label(window, text='До сортировки', font=("Times New "
- "Roman",
- 13))
- before_sort_label.place(x=93, y=5, width = 110)
- before_sort = Button(window, text=before, font=("Times New Roman",
- 13),
- command=before_sort_func)
- before_sort.place(x=5, y=35, width=290, height=20)
- after = ''
- for i in range(len(data)):
- key = data[i]
- lo, hi = 0, i
- while lo < hi:
- mid = lo + (hi - lo) // 2
- if key < data[mid]:
- hi = mid
- else:
- lo = mid + 1
- for j in range(i, lo, -1):
- data[j] = data[j - 1]
- data[lo] = key
- for i in range(len(data)):
- after += str(data[i]) + ' '
- after_sort_label = Label(window, text='После сортировки', font=("Times "
- "New "
- "Roman",
- 13))
- after_sort_label.place(x=85, y=60, width=130)
- after_sort = Button(window, text=after, font=("Times New Roman",
- 13),
- command=after_sort_func)
- after_sort.place(x=5, y=95, width=290, height=20)
- window.mainloop()
- def build_plot():
- N1 = inp_window_4.get()
- N2 = inp_window_5.get()
- if ('-' in N1 or N1.isdigit() == False or int(N1) <= 0):
- messagebox.showerror('Ошибка ввода', 'Введен неверный размер массива'
- ' N1')
- inp_window_1.delete(0, END)
- return
- if ('-' in N2 or N2.isdigit() == False or int(N2) <= 0):
- messagebox.showerror('Ошибка ввода', 'Введен неверный размер массива'
- ' N2')
- inp_window_2.delete(0, END)
- return
- N1 = int(N1)
- N2 = int(N2)
- array = []
- for i in range(min(N1, N2)):
- array.append(randint(-200, 200))
- start = perf_counter()
- for i in range(len(array)):
- key = array[i]
- lo, hi = 0, i
- while lo < hi:
- mid = lo + (hi - lo) // 2
- if key < array[mid]:
- hi = mid
- else:
- lo = mid + 1
- for j in range(i, lo, -1):
- array[j] = array[j - 1]
- array[lo] = key
- stop = perf_counter()
- time_plot = []
- size_plot = []
- time_now = stop - start
- size_now = min(N1, N2)
- for i in range(10):
- time_plot.append(time_now)
- size_plot.append(size_now)
- size_now += 1000
- time_now += time_now * pow(size_now/(size_now - 1000), 2)
- print(time_plot)
- print(size_plot)
- plt.title('Размер к времени')
- plt.grid()
- plt.xlabel('Размер')
- plt.ylabel('Время')
- plt.figure()
- plt.plot(size_plot, time_plot)
- plt.show()
- def build_table():
- N1 = inp_window_1.get()
- N2 = inp_window_2.get()
- N3 = inp_window_3.get()
- if ('-' in N1 or N1.isdigit() == False or int(N1) <= 0):
- messagebox.showerror('Ошибка ввода', 'Введен неверный размер массива'
- ' N1')
- inp_window_1.delete(0, END)
- return
- if ('-' in N2 or N2.isdigit() == False or int(N2) <= 0):
- messagebox.showerror('Ошибка ввода', 'Введен неверный размер массива'
- ' N2')
- inp_window_2.delete(0, END)
- return
- if ('-' in N3 or N3.isdigit() == False or int(N3) <= 0):
- messagebox.showerror('Ошибка ввода', 'Введен неверный размер массива'
- ' N3')
- inp_window_3.delete(0, END)
- return
- N1 = int(N1)
- N2 = int(N2)
- N3 = int(N3)
- size = []
- size.append(N1)
- size.append(N2)
- size.append(N3)
- sum = N1 + N2 + N3
- middle = sum - max(N1,N2,N3) - min(N1,N2,N3)
- table_root = Tk()
- table_root.title = ("Таблица сравнения")
- table_root.geometry('512x100')
- tree = tk.Treeview(table_root)
- tree['columns'] = ('one', 'two', 'three')
- tree.column('#0', width=212, minwidth=140)
- tree.column('one', width=100, minwidth=100)
- tree.column('two', width=100, minwidth=100)
- tree.column('three', width=100, minwidth=100)
- tree.heading('#0', text = '', anchor = CENTER)
- tree.heading('one', text = str(min(N1,N2,N3)), anchor = CENTER)
- tree.heading('two', text = str(middle), anchor = CENTER)
- tree.heading('three', text = str(max(N1,N2,N3)), anchor = CENTER)
- tree.pack()
- array = []
- for i in range(max(N1, N2, N3)):
- array.append(i)
- start_1 = perf_counter()
- start_2 = perf_counter()
- start_3 = perf_counter()
- for i in range(len(array)):
- if (i == min(N1, N2, N3)):
- stop_1 = perf_counter()
- if (i in size and i != min(N1, N2, N3) and i != max(N1, N2, N3)):
- stop_2 = perf_counter()
- key = array[i]
- lo, hi = 0, i
- while lo < hi:
- mid = lo + (hi - lo) // 2
- if key < array[mid]:
- hi = mid
- else:
- lo = mid + 1
- for j in range(i, lo, -1):
- array[j] = array[j - 1]
- array[lo] = key
- stop_3 = perf_counter()
- tree.insert('', 'end', text='Упорядоченный массив', tags=('ttk'),
- values=('{:0.6e}'.format(stop_1 - start_1),
- '{:0.6e}'.format(stop_2 - start_2),
- '{:0.6e}'.format(stop_3 - start_3)))
- array.clear()
- array_1 = []
- array_2 = []
- array_3 = []
- for i in range(max(N1, N2, N3)):
- number = randint(-500, 500)
- if i < min(N1, N2, N3):
- array_1.append(number)
- if i < middle:
- array_2.append(number)
- array_3.append(number)
- start_1 = perf_counter()
- for i in range(len(array_1)):
- key = array_1[i]
- lo, hi = 0, i
- while lo < hi:
- mid = lo + (hi - lo) // 2
- if key < array_1[mid]:
- hi = mid
- else:
- lo = mid + 1
- for j in range(i, lo, -1):
- array_1[j] = array_1[j - 1]
- array_1[lo] = key
- stop_1 = perf_counter()
- start_2 = perf_counter()
- for i in range(len(array_2)):
- key = array_2[i]
- lo, hi = 0, i
- while lo < hi:
- mid = lo + (hi - lo) // 2
- if key < array_2[mid]:
- hi = mid
- else:
- lo = mid + 1
- for j in range(i, lo, -1):
- array_2[j] = array_2[j - 1]
- array_2[lo] = key
- stop_2 = perf_counter()
- start_3 = perf_counter()
- for i in range(len(array_3)):
- key = array_3[i]
- lo, hi = 0, i
- while lo < hi:
- mid = lo + (hi - lo) // 2
- if key < array_3[mid]:
- hi = mid
- else:
- lo = mid + 1
- for j in range(i, lo, -1):
- array_3[j] = array_3[j - 1]
- array_3[lo] = key
- stop_3 = perf_counter()
- tree.insert('', 'end', text='Случайный массив', tags=('ttk'),
- values=('{:0.6e}'.format(stop_1 - start_1),
- '{:0.6e}'.format(stop_2 - start_2),
- '{:0.6e}'.format(stop_3 - start_3)))
- array_1.clear()
- array_2.clear()
- array_3.clear()
- for i in range(max(N1, N2, N3), 0, -1):
- if i > max(N1, N2, N3) - min(N1, N2, N3):
- array_1.append(i)
- if i > max(N1, N2, N3) - middle:
- array_2.append(i)
- array_3.append(i)
- start_1 = perf_counter()
- for i in range(len(array_1)):
- key = array_1[i]
- lo, hi = 0, i
- while lo < hi:
- mid = lo + (hi - lo) // 2
- if key < array_1[mid]:
- hi = mid
- else:
- lo = mid + 1
- for j in range(i, lo, -1):
- array_1[j] = array_1[j - 1]
- array_1[lo] = key
- stop_1 = perf_counter()
- start_2 = perf_counter()
- for i in range(len(array_2)):
- key = array_2[i]
- lo, hi = 0, i
- while lo < hi:
- mid = lo + (hi - lo) // 2
- if key < array_2[mid]:
- hi = mid
- else:
- lo = mid + 1
- for j in range(i, lo, -1):
- array_2[j] = array_2[j - 1]
- array_2[lo] = key
- stop_2 = perf_counter()
- start_3 = perf_counter()
- for i in range(len(array_3)):
- key = array_3[i]
- lo, hi = 0, i
- while lo < hi:
- mid = lo + (hi - lo) // 2
- if key < array_3[mid]:
- hi = mid
- else:
- lo = mid + 1
- for j in range(i, lo, -1):
- array_3[j] = array_3[j - 1]
- array_3[lo] = key
- stop_3 = perf_counter()
- tree.insert('', 'end', text='Обратно упорядоченный массив',
- tags=('ttk'),
- values=('{:0.6e}'.format(stop_1 - start_1),
- '{:0.6e}'.format(stop_2 - start_2),
- '{:0.6e}'.format(stop_3 - start_3)))
- array_1.clear()
- array_2.clear()
- array_3.clear()
- table_root.mainloop()
- root = Tk()
- root.title('Сортировка вставками с бинарным поиском')
- root.geometry('320x175')
- inp_1 = StringVar()
- inp_2 = StringVar()
- inp_3 = StringVar()
- inp_4 = StringVar()
- inp_5 = StringVar()
- left_label = Label(text = 'Левая граница', font = ("Times New Roman", 12))
- left_label.place(x = 20, y = 15)
- right_label = Label(text = 'Правая граница', font = ("Times New Roman", 12))
- right_label.place(x = 180, y = 15)
- inp_window_4 = Entry(root, textvariable = inp_4, font = (
- "Times New Roman", 13))
- inp_window_4.place(x = 20, y = 40, width = 110, height = 20)
- inp_window_5 = Entry(root, textvariable = inp_5, font = (
- "Times New Roman", 13))
- inp_window_5.place(x = 180, y = 40, width = 110, height = 20)
- N1_label = Label(text = 'N1', font = ("Times New Roman", 12))
- N1_label.place(x = 40, y = 75)
- N1_label = Label(text = 'N2', font = ("Times New Roman", 12))
- N1_label.place(x = 150, y = 75)
- N1_label = Label(text = 'N3', font = ("Times New Roman", 12))
- N1_label.place(x = 260, y = 75)
- inp_window_1 = Entry(root, textvariable = inp_1, font = (
- "Times New Roman", 13))
- inp_window_1.place(x = 20, y = 105, width = 60, height = 20)
- inp_window_2 = Entry(root, textvariable = inp_2, font = (
- "Times New Roman", 13))
- inp_window_2.place(x = 130, y = 105, width = 60, height = 20)
- inp_window_3 = Entry(root, textvariable = inp_3, font = (
- "Times New Roman", 13))
- inp_window_3.place(x = 240, y = 105, width = 60, height = 20)
- plot = Button(root, text = 'Plot', font = ("Times New Roman", 13), command =
- build_plot)
- plot.place(x = 20, y = 140, width = 60, height = 20)
- table = Button(root, text = 'Table', font = ("Times New Roman", 13), command =
- build_table)
- table.place(x = 240, y = 140, width = 60, height = 20)
- example = Button(root, text = 'Sort example', font = ("Times New Roman", 13),
- command = sort_example)
- example.place(x = 100, y = 140, width = 120, height = 20)
- inp_window_1.insert(END, 10)
- inp_window_2.insert(END, 100)
- inp_window_3.insert(END, 1000)
- inp_window_4.insert(END, 1000)
- inp_window_5.insert(END, 10000)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement