這個程序使用插入排序遞歸排序列表... 有人可以讓我明白'isort'是如何遞歸工作的,以及即使'isort'遞歸後'insert'如何運行,isort遞歸直到它已經完成了一次?python遞歸函數深度
def insertion(seq):
isort(seq,len(seq))
def isort(seq,k):
if k>1:
isort(seq,k-1)
insert(seq,k-1)
def insert(seq,k):
pos=k
while pos>0 and seq[pos]<seq[pos-1]:
(seq[pos],seq[pos-1])=(seq[pos-1],seq[pos])
pos=pos-1
不確定是否t他解釋它,但相關:https://www.youtube.com/watch?v=ROALU379l3U –
嗯,這段代碼確實是插入排序,但分配操作不必要地交換。 –
是它的不必要的,它只是一個遞歸的例子 –