-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminmax.py
More file actions
48 lines (48 loc) · 1.35 KB
/
minmax.py
File metadata and controls
48 lines (48 loc) · 1.35 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
# -*- coding: utf-8 -*-
import random
import time
def min_max_method1(search_data):#穷举搜索
length=len(search_data)
temp_max=search_data[1]
temp_min=search_data[1]
for i in range(2,length):#从第二个元素进行对比
if search_data[i]>temp_max:
temp_max=search_data[i]
if search_data[i]<temp_min:
temp_min=search_data[i]
return temp_max,temp_min
def min_max_method2(search_data,low,high):#分治法进行搜索函数
#the situation of only two number
if high-low<=1:#当只有1个或2个元素时
if search_data[low]<search_data[high]:
return (search_data[high],search_data[low])
else:
return (search_data[low],search_data[high])
#the situation of more number than 2
mid_num=int((low+high)/2)
x1,y1=min_max_method2(search_data,low,mid_num)
x2,y2=min_max_method2(search_data,mid_num+1,high)
#比较大小
if x1<x2:
x=x2
else:
x=x1
#比较大小
if y1<y2:
y=y1
else:
y=y2
return x,y
if __name__=="__main__":
range_upper=200
length=15
list_search=random.sample(range(range_upper),length)
print('Before search:',list_search)
starttime=time.time()
print(min_max_method1(list_search))
endtime=time.time()
print("function1 time consuming:",(endtime-starttime),"s")
starttime=time.time()
print(min_max_method2(list_search,0,len(list_search)-1))
endtime=time.time()
print("function2 time consuming:",(endtime-starttime),"s")