What value of q does PARTITION return when all elements in the array A[p..r] have the same value? Modify PARTITION so that $q = \lfloor(p + r)/2\rfloor$ when all elements in the array A[p..r] have the same value.
At the end of the for loop of the PARTITION subroutine the value of i in a array that has same values is q -1 and it will return q. We can modify the PARTITION line 4 as
A[j] < x or A[j] == x and j%2 == 0 this will return $q = \lfloor(p + r)/2\rfloor$ if you have an array with same values.
No comments:
Post a Comment