擅长:python、mysql、java
<p>你必须改变这一点:</p>
<pre><code> elif ord(word[num])==ord(wordleft[num]):
num=num+1
word_checker(bookwords, num, i)
else:
</code></pre>
<p>收件人:</p>
^{pr2}$
<p>然后它将打印:<code>['maddy', 'mega', 'money', 'michael', 'michelle', 'miniscus', 'monstor', 'mountain']</code></p>
<p>无论如何,我看不出递归的意义,我认为插入排序不做递归。在</p>
<h2>更新</h2>
<p>当按字符比较时,算法被破坏,但是python可以为您比较字符串,因此这将给出正确的结果:</p>
<pre><code>def insertion_sort(bookwords):
for index in range(1,len(bookwords)):
global word
word=bookwords[index]
i=index-1
word_checker(bookwords, i)
def word_checker(bookwords, i):
while i>=0:
wordleft=bookwords[i]
if word<wordleft:
bookwords[i+1]=bookwords[i]
bookwords[i]=word
i=i-1
bookwords=["michael", "maddy", "michelle", "monstor", "money", "mountain", "miniscus", "mega"]
insertion_sort(bookwords)
print bookwords #prints ['maddy', 'mega', 'michael', 'michelle', 'miniscus', 'money', 'monstor', 'mountain']
</code></pre>