i have written a recursive randomized quick sort function as below:
def randomized_quick_sort(a, l, r):
if l >= r:
return
k = random.randint(l, r)
a[l], a[k] = a[k], a[l]
m1, m2 = partition3(a, l, r)
randomized_quick_sort(a,l,m1-1)
randomized_quick_sort(a,m2+1,r)
the partition function used is given below which partitions a list into three parts, less than pivot, equal to pivot, and greater than pivot where pivot is the first element in the input list.
def partition3(a, l, r):
x = a[l]
less, equal, greater = [], [], []
for val in a[l:r+1]:
if val < x:
less.append(val)
if val == x:
equal.append(val)
if val > x:
greater.append(val)
a[l:r+1] = less + equal + greater
m1 = len(less)
m2 = m1 + len(equal) - 1
return m1, m2
if i run this quicksort function several times on a simple input such as
a = [2,2,3,3]
randomized_quick_sort(a,0,len(a)-1)
after only a few trials i get a "maximum recursion depth exceeded" error. Please help!