import sys from time import clock def main(): start=clock() try: f=open("7_1.txt","r") f1=open("out_34_sec7_1.txt","w") temp=[] ncomp=0 #To count the number of comparisons. append=temp.append #In python, local variables are easier to access than global ones. If temp.append() is invoked multiple times, it will call the append function from the library everytime. #Therefore, using local variable, append() will be call from library just once and later using the local variable.""" write=f1.write for x in f: for y in x.split(','): append(y) f.close() n=int(temp[0]) temp=temp[1::] a1=int(temp[0]) #Now temp[0] corresponds to the first element of the main list. a2=int(temp[1]) max=a1 if n%2==0: #This is the 'tournament' algorithm. if a1>a2: max=a1 ncomp+=1 else: max=a2 i=2 else: max=a1 i=1 while iq: ncomp+=1 if p>max: max=p ncomp+=1 else: if q>max: max=q ncomp+=1 i+=2 write("%d" %max) end=clock() write("\nThere are %d comparisons in this program." %ncomp) write("\nTime taken by the program is : %f seconds" %(end-start)) mem=sys.getsizeof(f)+sys.getsizeof(f1)+sys.getsizeof(temp)+sys.getsizeof(append)+sys.getsizeof(write)+sys.getsizeof(x)+sys.getsizeof(y)+sys.getsizeof(n)+sys.getsizeof(a1)+sys.getsizeof(a2)+sys.getsizeof(max)+sys.getsizeof(i)+sys.getsizeof(p)+sys.getsizeof(q) write("\nMemory used by the program is : %d bytes." %mem) f1.close() except IOError: print "Input file not found in the directory." main()