写递归时报错:RecursionError: maximum recursion depth exceeded in comparison
class Solution:def partition(self,a,left,right):pivot=left # 划分参考数索引,默认为第一个数为基准数,可优化while (left <right):while (left<right and a[right]>=a[pivot]):right-=1while (left<right and a[left]<=a[pivot]):left+=1a[left],a[right]=a[right],a[left]return leftdef quick_sort(self,a,left,right):if left>=right:returnmid=self.partition(a,left,right)self.quick_sort(a,left,mid-1)self.quick_sort(a,mid+1,right)
目前出现的原因是:递归中少了判断返回的判断,即我忘了写下面两行:
if left>=right:return