im not that familiar with the concept of recursion, and im not sure if these codes have recursion in them or use recursion.
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
if it's included, pointing out where it is would be extremely helpful.
The definition of a recursive function is one that calls itself within itself.
Furthermore, the only function that does this in your code is the second one:
def quickSortHelper(alist,first,last):
if first<last:
splitpoint = partition(alist,first,last)
quickSortHelper(alist,first,splitpoint-1) # Calls itself here
quickSortHelper(alist,splitpoint+1,last) # and here
So, the second function is recursive; the other two are not.
Note: As @Sean said in the comment below, the definition I gave for recursion is a very simplified one. There are other code structures (such as two or more methods alternately calling one another) that can be classified as using recursion. For a complete overview of recursion, please see the link I gave above.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments