擅长:python、mysql、java
<p>适当的方法是插入排序算法,但现在我们只对最后一项进行排序,所以,这里是:</p>
<pre><code>list = [1, 3, 4, 5, 6, 2] # our list
item = list[len(list)-1] # last element
i = len(list)-2 # the starting element to compare the last element to
while item<list[i] and i>=0: # while place not found and index in range
list[i+1]=list[i] # move element at i to i+1
i-=1 # decrement i, so to compare with next left element
list[i+1]=item # when the loop is completed, we then have our last item's position in i+1
print(list) # this prints [1, 2, 3, 4, 5, 6]
</code></pre>
<p><br/>
您可以阅读更多关于插入排序算法的内容,这里的解释有点棘手,我的意思是它需要一个很长的解释和示例,因此,您可以在Wikipedia上看到更多:<a href="https://en.wikipedia.org/wiki/Insertion_sort" rel="nofollow">https://en.wikipedia.org/wiki/Insertion_sort</a></p>