嵌套递归方法调用

时间:2015-11-18 22:19:44

标签: python recursion

嵌套递归方法调用如何在流程中工作?在下面的quicksort python代码中,quickSortHelper递归函数使用参数alist,first和splitpoint-1调用自己一次。然后它再次用alist,splitpoint + 1,last调用自己。进行这些调用后,程序如何流动?对我来说,看起来永远不会达到第二个递归方法调用。

def quickSort(alist):
   quickSortHelper(alist,0,len(alist)-1)

def quickSortHelper(alist,first,last):
   if first<last:

       splitpoint = partition(alist,first,last)

       quickSortHelper(alist,first,splitpoint-1)
       quickSortHelper(alist,splitpoint+1,last)


def partition(alist,first,last):
   pivotvalue = alist[first]

   leftmark = first+1
   rightmark = last

   done = False
   while not done:

       while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
           leftmark = leftmark + 1

       while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
           rightmark = rightmark -1

       if rightmark < leftmark:
           done = True
       else:
           temp = alist[leftmark]
           alist[leftmark] = alist[rightmark]
           alist[rightmark] = temp

   temp = alist[first]
   alist[first] = alist[rightmark]
   alist[rightmark] = temp


   return rightmark

alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist)
print(alist)

1 个答案:

答案 0 :(得分:1)

你的问题不是关于python代码,而是关于递归 !!  首先让我解释当你调用函数时会发生什么,比如f(),递归! 首先让我们看一下你可能已经看过很多的简单电话:
1-普通电话:你肯定已经面临很多了!这很难吗?

def g(num):
    return 2*num

def h(num):
    return 2+num

def f(num):
    return g(num)+h(num)

2-再次正常通话:这次代替两个不同的功能(h&amp; g)我们调用2个相同的函数(g)但与调用函数(f)不同:这也是容易!

def g(num):
    return 2*num

def f(num):
    return g(num)+g(num)

3-递归来电!!! : 现在我们使用与调用者相同的函数来代替不同的调用:

def f(num):
    if num <0 :
        return 0
    return 2+f(num-1)+f(num-2)

所以如果我们想要计算f(num),我们必须知道f(num-1)和f(num-2)。所以我们必须调用它们的函数并将它们加在一起。重要的是,在我们之后calucated f(num-1)-say等于10-我们在return语句中:

返回2 + 10 + f(num-2)

所以return语句意味着,回到你被叫了! f(num-1)返回f(num)调用它! 不要混淆return和exit!

我还为你的代码添加了一些跟踪点,看看会发生什么以及运行的函数的顺序是什么!

count = 0

def quickSort(alist):
  global count
  print "quickSort() and count is",count
  count+=1
  quickSortHelper(alist,0,len(alist)-1)

def quickSortHelper(alist,first,last):
   global count
   print "quickSortHelper() and count is",count
   count+=1
   if first<last:

       splitpoint = partition(alist,first,last)

       print "quickSortHelper(),counting first part"
       quickSortHelper(alist,first,splitpoint-1)
       print "quickSortHelper(),done first part"
       print '#'*10
       print
       print "quickSortHelper(),counting first part"
       quickSortHelper(alist,splitpoint+1,last)


def partition(alist,first,last):
   global count
   print "partition() and count is",count
   count+=1
   pivotvalue = alist[first]

   leftmark = first+1
   rightmark = last

   done = False
   while not done:

       while leftmark <= rightmark and alist[leftmark] <= pivotvalue:
           leftmark = leftmark + 1

       while alist[rightmark] >= pivotvalue and rightmark >= leftmark:
           rightmark = rightmark -1

       if rightmark < leftmark:
           done = True
       else:
           temp = alist[leftmark]
           alist[leftmark] = alist[rightmark]
           alist[rightmark] = temp

   temp = alist[first]
   alist[first] = alist[rightmark]
   alist[rightmark] = temp


   return rightmark

alist = [54,26,93,17,77,31,44,55,20]
quickSort(alist)
print(alist)