-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinary_search.py
More file actions
67 lines (59 loc) · 1.77 KB
/
Binary_search.py
File metadata and controls
67 lines (59 loc) · 1.77 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
#coding:UTF-8
#Author: Toryun
#Date: 2020-04-05 21:42:00
#Function: Binary search
import math
import time
import numpy
import os
def Binary_search(listdata,value):
low = 0
high = len(listdata) - 1
if len(listdata) == 0:
print "{} is None".format(listdata)
return high
elif not isinstance(listdata,list):
print "{} is not list type".format(listdata)
if not isinstance(value,int) or isinstance(value,float):
print "{} is not int or float type".format(value)
t0 = time.time()
mid = (high - low)/2
while mid>-1:
if listdata[mid] == value:
print "{} is in {}th postion".format(value,mid+1)
break
elif listdata[mid] < value:
low = mid + 1
mid = (high - low) / 2 + low
else:
high = mid -1
mid = (high - low) / 2
print mid
t1 = time.time()
T = t1 - t0
#print "Binary_search Total time is {}".format(T)
return T
def regular_search(listdata,value):
t0 = time.time()
for i in range(len(listdata)):
if listdata[i] == value:
print "We find this value in {}th postion".format(i+1)
t1 = time.time()
T = t1 - t0
print "Regular_search Total time is {}".format(T)
return T
if __name__ =='__main__':
d = [i for i in xrange(1,1000)]
#v = int(raw_input('Please input the value you want to search:\n'))
l0 = []
l1 = [0,0,0]
for v in range(1,1000):
p0 = Binary_search(d,v)
p1 = regular_search(d,v)
p = p1 - p0
l1[0],l1[1],l1[2]= p0,p1,p
l0.append(l1)
with open(os.getcwd()+'/binary_searchTest.txt','a+') as f:
for i in range(len(l0)):
f.write(str(l0[i]))
f.close()