-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex3.2.py
68 lines (45 loc) · 1.17 KB
/
ex3.2.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
import json
import random
import time
import matplotlib.pyplot as plt
x = open('ex2tasks.json')
y = open('ex2data.json')
tasks = json.load(x)
data = json.load(y)
print (len(data))
def binary_search(arr, target, start):
left, right = 0, len(arr) - 1
mid = start
while left <= right:
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
mid = (left + right) // 2
return -1
midpoint = []
taskstr = []
for task in tasks:
taskstr.append(str(task))
points = []
times = []
for i in range(0,100):
point = random.randint(0, 999999)
start_time = time.time()
binary_search(data,task,point);
end_time = time.time()
times.append(end_time-start_time)
points.append(str(point))
datadict = dict(zip(points,times))
datasort = sorted(datadict)
midpoint.append(int(datasort[0]))
plt.scatter(taskstr,midpoint)
plt.xlabel("Tasks")
plt.ylabel("Starting Midpoints")
plt.title("Midpoints vs Tasks")
plt.xticks(rotation=90)
plt.show()
x.close()
y.close()