Message-ID: <f5dv4o$t1c$3@news.cn99.com>
Subject: =?UTF-8?B?cmV0dXJu6K+t5Y+l5byV6LW355qE54G15byC5LqL5Lu2?=
Date: Thu, 21 Jun 2007 20:46:50 +0800
From: XiaQ <xiaqqaix@163.com>
Path: news.cnusenet.org!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!mbox2nntp-cn.bbs.comp.lang.python.mbox.zip!g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!213.200.89.82.MISMATCH!tiscali!newsfeed1.ip.tiscali.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!news.cn99.com!not-for-mail
Newsgroups: cn.bbs.comp.lang.python
Organization: Bentium Ltd. (CN99)
Lines: 39
NNTP-Posting-Host: 121.46.128.3
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.cn99.com 1182433240 29740 121.46.128.3 (21 Jun 2007 13:40:40 GMT)
X-Complaints-To: usenet@news.cn99.com
NNTP-Posting-Date: Thu, 21 Jun 2007 13:40:40 +0000 (UTC)
User-Agent: Thunderbird 2.0.0.4 (Windows/20070604)
Xref: news.cnusenet.org cn.bbs.comp.lang.python:37744

我在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