-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy path6e PSG (sort and search with listbox).py
137 lines (122 loc) · 4.03 KB
/
6e PSG (sort and search with listbox).py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#PySimple examples (v 3.9)
#Tony Crewe
#Oct 2018 MacOs
import FreeSimpleGUI as sg
sg.SetOptions(background_color = 'DarkGrey',
element_background_color = 'DarkGrey',
text_element_background_color = 'DarkGrey',
font= ('Calibri', 14, 'bold'))
#setup column (called column1) of buttons to use in layout
column1 = [[sg.ReadButton('Original list', size = (10,1))],
[sg.ReadButton('Default sort', size = (10,1))],
[sg.ReadButton('Sort: selection',size = (10,1))],
[sg.ReadButton('Sort: quick', size = (10,1))]]
layout =[[sg.Text('Search and Sort Demo', font =('Calibri', 20, 'bold'))],
[sg.Listbox(values =[''], size = (14, 11),font = ('Calibri', 12), background_color ='White',key = '_display_'), sg.Column(column1)],
[sg.Text('_'*38,font = ('Calibri', 16))],
[sg.InputText(size = (10,1), key = '_linear_'), sg.Text(' '), sg.InputText(size = (11,1), key = '_binary_')],
[sg.ReadButton('Linear Search', size = (11,1)), sg.Text(' '), sg.ReadButton('Binary Search', size = (11,1))],
]
window = sg.Window('Search and Sort Demo').Layout(layout)
names= ['Roberta', 'Kylie', 'Jenny', 'Helen',
'Andrea', 'Meredith','Deborah','Pauline',
'Belinda', 'Wendy']
#function to display list
def display_list(list):
global list_displayed
#store list in Multiline text globally
list_displayed = list
#add list elements with new line
values = [l for l in list]
window.FindElement('_display_').Update(values)
#use inbuilt python sort
def default(names):
l = names[:]
l.sort()
display_list(l)
#Selection sort
def sel_sort(names):
l = names[:]
for i in range(len(l)):
smallest = i
for j in range(i+1, len(l)):
if l[j] < l[smallest]:
smallest = j
l[smallest], l[i] = l[i], l[smallest]
display_list(l)
#Quick sort
def qsort_holder(names):
l = names[:]
quick_sort(l, 0, len(l) - 1)
display_list(l)
def quick_sort(l, first, last):
if first >= last:
return l
pivot = l[first]
low = first
high = last
while low < high:
while l[high] > pivot:
high = high -1
while l[low] < pivot:
low = low + 1
if low <= high:
l[high], l[low] = l[low], l[high]
low = low + 1
high = high -1
quick_sort(l, first, low -1)
quick_sort(l, low, last)
#Linear Search - no need for Ordered list
def linear_search():
l = names[:]
found = False
for l in l:
if l == value['_linear_']:
found = True
#Create list for display
result = ['Linear search', l + ' found']
window.FindElement('_display_').Update(result)
break
if not found:
#Create list for display
result = [value['_linear_'], 'was not found']
window.FindElement('_display_').Update(result)
#Binary Search
def binary_search():
l = list_displayed[:]
lo = 0
hi = len(l)-1
found = False
while lo <= hi:
mid = (lo + hi) //2
if l[mid] == value['_binary_']:
#Create list for display
result = ['Binary search', l[mid] + ' found.']
window.FindElement('_display_').Update(result)
found = True
break
elif l[mid] < value['_binary_']:
lo = mid + 1
else:
hi = mid - 1
if not found:
#Create list for display
result = [value['_binary_'], 'was not found']
window.FindElement('_display_').Update(result)
while True:
button, value = window.Read()
if button is not None:
if button == 'Original list':
display_list(names)
if button == 'Default sort':
default(names)
if button == 'Sort: selection':
sel_sort(names)
if button == 'Sort: quick':
qsort_holder(names)
if button == 'Linear Search':
linear_search()
if button == 'Binary Search':
binary_search()
else:
break