Is there an in-place partitioning algorithm (of the kind used in a Quicksort implementation) that does not rely on the pivot element being present in the array?
In other words, the array elements must be arranged in this order:
- Elements less than the pivot (if any)
- Elements equal to the pivot (if any)
- Elements greater than the pivot (if any)
It must still return the index (after sorting) of the pivot element if it happens to be present in the array, or a special value if not; This could be the one's complement of an index where the element could be inserted to maintain the order (like the return value of Java's standard binary search function.)
The implementations I have seen require the index of the pivot element to be given as a parameter (or always to be at the start of the array.) Unfortunately I do not know in advance whether the pivot is present in the array (or where it is in the array.)
Edit (in reply to meriton's comments): We can also assume that one of the following conditions is true:
- The array length is < 2, or
- At least one element is <= pivot and at least one element is >= pivot.
There may be duplicate values in the array (including duplicates of the pivot value.)