我想要做的是提示用户排序函数类型,排序模式,数组大小,数组增量大小和测试次数。然后我想要它来保存它。但是,这个程序有几个问题。Python中的排序模式算法
不知怎的,当我选择了随机模式也给了我一些奇怪的答案,如:
1543 0.002
600 0.020
1400 0.08
它不是真的按顺序。我认为用for循环会出现问题。
def rand_array(n):
''' returns sorted array of integers of size n'''
R=[randint(1, 1000*n) for i in xrange(n)]
return R
def sorted_array(n):
''' returns a sorted array of n integers'''
return [i for i in xrange(1,n+1)]
def rev_array(n):
'''returns an array of n integers in reverse order'''
R= [i for i in reversed(xrange(1,n+1))]
return R
def sort_timehelp(x,f):
''' This times the quick sort algorithm as it must take 3 variables'''
high=len(x)
low=0
t0=clock()
f(x,low,high)
t1=clock()
dt=t1-t0
return dt
def main():
myinfo()
info()
while True:
print '==================== to quit enter Control-c=================='
sortfunction=input("Choose a sort function: ")
s=input("Choose a pattern: ")
n=input("Array Size: ")
increment=input("Increment size: ")
y=input("Number of tests: ")
if s == 1:
x=rand_array(n)
elif s ==2:
x= sorted_array(n)
elif s==3:
x=rev_array(n)
if sortfunction==1:
i=0
output="algorith: quick sort \n input data: %s" %s
print output
while i<y:
i=i+1
ff=0.0
array=x[increment-1:n:increment]
for my in array:
ff+=sort_timehelp(x,quick_sort)
output="%d\t %f" %(my, ff)
print output
saving=input("You want to save data ? type 0 to continue or 1 to save ")
if saving == 0:
continue
if saving == 1:
ask=raw_input("Type the name file: ")
fileout=open(ask+".csv","w")
fileout.write(output)
fileout.close()
第二个问题是,当我试图保存数据它只保存最后的数据,但我想保存一切。
我将不胜感激任何帮助。
编辑: 计时函数采取和数组和排序算法0我想通过增量和相应的时间来保存数字。 (那是我的for循环)
有几点意见:** 1)**为什么你将两个参数传递给你的sort函数sortfun(x,n)'? ** 2)**您的文档字符串实际上并不描述您的功能。 ** 3)**当你说你想保存所有内容时,你的意思是在'while True'的每个分支内还是'output'的每个实例内? – bossylobster
'output ='%d \ t%f“%(my,ff) print output'后面的行没有正确缩进。 –
indentations are correct –