I was trying to implement the in place partition subroutine of quicksort.It works with array of unique elements,but fails when the array contains duplicate elements
The code goes like this
def inplace_partitioning(input,l,r):
len_a=len(input)
pivot=input[l]
i=l+1
for j in range(l+1,r+1,1):
if input[j]<pivot:#do nothing if new elem >pivot
#swap new elem with leftmost elem greater than pivot
temp=input[j]
input[j]=input[i]
input[i]=temp
i+=1
#swap pivot with rightmost elem lessthan pivot
temp=input[l]
input[l]=input[i-1]
input[i-1]=temp
When the input is of unique elements,the code gives correct results
input=[5,6,3,1,8,4]
inplace_partitioning(input,0,len(input)-1)
print input
>>[4, 3, 1, 5, 8, 6]
when I tried the array below,i got wrong results
input=[5,6,3,1,8,5]
>>>[1, 3, 5, 6, 8, 5]
Is this because my implementation faulty?can someone help out a bit?