forked from pritikmshaw/Python-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindMax.py
More file actions
42 lines (34 loc) · 1.04 KB
/
findMax.py
File metadata and controls
42 lines (34 loc) · 1.04 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
import random
import matplotlib.pyplot as plt
import time
class FindMax(object):
def __init__(self,arr):
self.arr = arr
def iterative_approach(self):
maxItem = self.arr[0]
for i in range(len(self.arr)):
if(maxItem<self.arr[i]):
maxItem = self.arr[i]
return maxItem
def recursive_approach(self,beg,end):
if(end==beg):
return self.arr[beg]
max1 = self.recursive_approach(beg,(beg+end)//2)
max2 = self.recursive_approach(((beg+end)//2)+1,end)
if(max1>max2):
return max1
else:
return max2
#driver code
arr = []
for i in range(1000):
arr.append(random.randint(0,10000))
max_object = FindMax(arr)
t1 = time.clock()
iter_answer = max_object.iterative_approach()
t2 = time.clock()
print(str(iter_answer)+"founded in "+str(t2-t1)+" seconds")
t1 = time.clock()
recur_answer = max_object.recursive_approach(0,len(arr)-1)
t2 = time.clock()
print(str(recur_answer)+"founded in "+str(t2-t1)+" seconds")