繁体   English   中英

python递归函数深度

[英]python recursion function depth

该程序使用插入排序对列表进行递归排序...有人可以让我理解'isort'的递归工作方式,以及即使在'isort'递归后'insert'也是如何运行的,isort递归是否被挂起直到完全运行一次?

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   

看看isort(seq, k)

该函数确保对最后k个元素进行排序

def isort(seq,k):
  if k>1:
    isort(seq,k-1)  
    // seq[k-1:] is sorted

    insert(seq,k-1)
    // the k'th item is now inserted in the correct place,
    // so seq[k:] is sorted

对于k=len(seq) ,对seq[k:]进行简单排序(这是一个空列表),递归在每一步中将k减1,从尾到头对seq进行排序

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM