generated from saidoyk/saidoyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
week9
155 lines (140 loc) · 5.43 KB
/
week9
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import random
import time
def seq_search(inplist, searched_list):
pos = 0
found = False
while pos < len(inplist) and not found:
if inplist[pos] == searched_list[0]:
if (pos+1) == len(inplist):
print("Position of searched sublist:None")
break
elif inplist[pos+1] == searched_list[1]:
found = True
print("Position of searched sublist:" ,pos)
else:
print("Position of searched sublist:None")
break
else:
pos = pos+1
test_list = [3,5,7,2,6,88,45,1,16,25]
searchlist =[7,2]
print(seq_search( test_list,searchlist))
def recur_seq_ordered_search( inplist, searched_list ):
pos = 0
found = False
stop = False
while pos < len(inplist) and not found and not stop:
if inplist[pos] == searched_list[0]:
if inplist[pos]> searched_list[0]:
stop = True
print("Position of searched sublist:None")
break
if (pos + 1) == len(inplist):
print("Position of searched sublist:None")
break
elif inplist[pos + 1] == searched_list[1]:
found = True
print("Position of searched sublist:", pos)
else:
print("Position of searched sublist:None")
break
else:
pos = pos + 1
test_list = [3,5,7,2,6,88,45,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,1,16,253,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,3,5,7,2,6,88,45,1,16,]
searchlist =[3,5]
print(seq_search( test_list,searchlist))
def iter_bin_search( inplist, searched_list ):
first= 0
last= len(inplist)-1
found = False
while first <= last and not found:
midpoint =( first + last) //2
if inplist[midpoint] == searched_list[0]:
if (midpoint+1) == len(inplist):
print("Position of searched sublist:None")
break
elif inplist[midpoint+1] == searched_list[1]:
found = True
print("Position of searched sublist:" ,midpoint)
else:
print("Position of searched sublist:None")
break
else:
if searched_list[0]< inplist[midpoint]:
last = midpoint -1
else:
first = midpoint+1
test_list = [3,5,7,2,6,88,45,3,5]
searchlist =[6,88]
print(iter_bin_search( test_list,searchlist))
def recur_bin_search(inplist,searched_list):
if len(inplist)==0:
return False
else:
midpoint = len(inplist)//2
if inplist[midpoint]==searched_list[0]:
if (midpoint+1) == len(inplist):
print("Position of searched sublist:None")
return True
elif inplist[midpoint+1] == searched_list[1]:
print("Position of searched sublist:", midpoint)
return True
else:
print("Position of searched sublist:None")
return True
else:
if searched_list[0] < inplist[midpoint]:
return recur_bin_search(inplist[:midpoint],searched_list)
else:
return recur_bin_search(inplist[midpoint+1:],searched_list)
test_list = [3,5,7,2,6,88,45,3,5]
searchlist =[6,88]
print(recur_bin_search( test_list,searchlist))
exec_time_seq_search = []
exec_time_recur_seq_search = []
exec_time_iter_bin_search = []
exec_time_recur_bin_search = []
for n in range(5, 38, 5):
inplist = random.sample(range(0, 10 * n), n)
ordered_inplist = sorted(inplist)
seq_row = []
recur_sec_row = []
iter_bin_row = []
recur_bin_row = []
for m in range(1, n // 2):
searchedlist = random.sample(range(0, 10 * m), m)
ordered_searchedlist = sorted(searchedlist)
t1 = time.time()
seq_search(inplist, searchedlist)
t2 = time.time()
seq_row.append((t2 - t1) * 1000) # Execution time in ms
t1 = time.time()
recur_seq_ordered_search(ordered_inplist, ordered_searchedlist)
t2 = time.time()
recur_sec_row.append((t2 - t1) * 1000)
t1 = time.time()
iter_bin_search(ordered_inplist, ordered_searchedlist)
t2 = time.time()
iter_bin_row.append((t2 - t1) * 1000)
t1 = time.time()
recur_bin_search(ordered_inplist, ordered_searchedlist)
t2 = time.time()
recur_bin_row.append((t2 - t1) * 1000)
exec_time_seq_search.append(seq_row)
exec_time_recur_seq_search.append(recur_sec_row)
exec_time_iter_bin_search.append(iter_bin_row)
exec_time_recur_bin_search.append(recur_bin_row)
def print2d(list2d):
for i in range(len(list2d)):
print('[', end=' ')
for j in range(len(list2d[i])):
print(list2d[i][j], end=' ')
print(']')
print("Execution time for iterative sequential search:")
print2d(exec_time_seq_search)
print("Execution time for recursive sequential search:")
print2d(exec_time_recur_seq_search)
print("Execution time for iterative binary search:")
print2d(exec_time_recur_seq_search)
print("Execution time for recursive binary search:")
print2d(exec_time_recur_bin_search)