2012-05-01 28 views
-3

我想要做的是提示用户排序函数类型,排序模式,数组大小,数组增量大小和测试次数。然后我想要它来保存它。但是,这个程序有几个问题。Python中的排序模式算法

  1. 不知怎的,当我选择了随机模式也给了我一些奇怪的答案,如:

    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循环)

+2

有几点意见:** 1)**为什么你将两个参数传递给你的sort函数sortfun(x,n)'? ** 2)**您的文档字符串实际上并不描述您的功能。 ** 3)**当你说你想保存所有内容时,你的意思是在'while True'的每个分支内还是'output'的每个实例内? – bossylobster

+0

'output ='%d \ t%f“%(my,ff) print output'后面的行没有正确缩进。 –

+0

indentations are correct –

回答

0

有很多问题。让我们通过他们...

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 rand_array(n): 
''' returns sorted array of integers of size n''' 
    return sorted([randint(1, 1000) for i in xrange(n)]) 

def sorted_array(n): 
    ''' returns a sorted array of n integers''' 
    return [i for i in xrange(1,n+1)] 

这应该仅仅是:

def sorted_array(n): 
    ''' returns a sorted array of n integers''' 
    return range(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 rev_array(n): 
    '''returns an array of n integers in reverse order''' 
    return reversed(sorted_array(n)) 

  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 

所以你选的次数(在内部循环),你有数组元素?不知道为什么。无论如何,与i生意应该简单地用for循环中完成:

  print "algorith: quick sort \n input data: %s" %s 
      for i in range(y): 
       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() 

if saving==0子句可以删除;除1以外的任何值saving都将跳过保存。

正如Scott指出的,您需要"a"而不是"w"open。你可以做的另一件事是将openclose移出循环。您可能还想使用内置的Python csv模块。

3

你的随机模式实际上是一个随机模式,而不是按照文档字符串的建议排序的列表。

要保存所有内容,请打开您的输出文件以进行追加,而不仅仅是写入(如您发现的那样,将覆盖以前的内容)。也就是说,使用“a”而不是“w”。