I tried to implement quick select to find the m'th smallest number in the list. When I run the program it returns the correct values sometime and incorrect values other times on the same array. What am I doing wrong her is the code
def select_mth_smallest(A, m):
pivot = np.random.choice(A)
# split into 3 partitions
A1 = []
A2 = []
A3 = []
for item in A:
if item < pivot:
A1.append(item)
if item > pivot:
A3.append(item)
else:
A2.append(item)
#find where m'th largest element is and recurse accordingly
if len(A1) >= m:
return select_mth_smallest(A1, m)
elif (len(A1) + len(A2)) >= m:
return pivot
else:
return select_mth_smallest(A3,m - (len(A1)+len(A2)))
Here is an input where the algorithm fails.
A = [1,2,3,4,5]
select_mth_smallest(A,5)
When I repeatedly execute this above statement I get, 5(correct) and 4(incorrect) alternatingly.
One thing that particularly baffles me (I am new to python) is that why I get different return values for the function when repeated with the same input. Looks rather sporadic.. BTW I am using Jupyter