私がやろうとしているのは、ユーザーに並べ替え関数の種類、並べ替えパターン、配列サイズ、配列増分のサイズ、およびテストの数を求めることです。それから私はそれを保存したい。ただし、このプログラムにはいくつかの問題があります。
どういうわけか、ランダムパターンを選択すると、次のような奇妙な答えが得られます。
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()
2 番目の問題は、データを保存しようとすると、最後のデータのみが保存されますが、すべてを保存したいということです。
助けていただければ幸いです。
編集:タイミング関数は、配列と並べ替えアルゴリズムを使用して、数値を増分とそれに対応するタイミングで保存したいと考えています。(それが私のforループです)