没发现问题,python2.5.1

"XiaQ" <xiaqqaix@163.com> 写入消息新闻:f5dv4o$t1c$3@news.cn99.com...
> 我在Python里实现插入排序,代码如下:
> def InsSort(li):
>     """ InsSort(li) -- Insertion Sort *IN PLACE*. """
>     for j in range(1, len(li)):
>         key = li[j]
>         i = j - 1
>         while i >=0 and li[i] > key:
>             li[i+1] = li[i]
>             i -= 1
>         li[i+1] = key
>     #return li
> 测试一切正常。但是,我在它的末尾加上了了一句
> return li(原代码中这一句被注释掉了)
> 却怎么都不对了。应该不是版本问题(我在Python 2.5,IronPython 1.1里都测试 
> 过了),望高手解答!
>
> 顺便写了一个测试suite:
> import random
> def RandomList(n = 10):
>     li = []
>     for i in range(n):
>         li.append(random.random())
>     return li
>
> def InsSort(li):
>     """ InsSort(li) -- Insertion Sort *IN PLACE*. """
>     for j in range(1, len(li)):
>         key = li[j]
>         i = j - 1
>         while i >=0 and li[i] > key:
>             li[i+1] = li[i]
>             i -= 1
>         li[i+1] = key
>     #return li
>
> if __name__ == '__main__':
>     li = RandomList()
>     InsSort(li)
>     print li