Divide-and-Conquer ALGORITHM HoarePartition(Al.r]) nt Partitions a subarray by H
ID: 3738345 • Letter: D
Question
Divide-and-Conquer ALGORITHM HoarePartition(Al.r]) nt Partitions a subarray by Hoare's algorithm, using the first ele as a pivot Inyput Suharray of aray A0.n- 11 defned by its let and righr Output: Partition of Al.'1, with the split position return p ? ?!] this function's value repeat p repeat i i+1 until Ali]2 repeat j +)-1 until A[j] p swap(Ali], AU) until i 2j swap(Ali], AU) /undo last swap when i 2 j swap(All, AU] return j Note that index i can go out of the subarray's bounds in this ps append to array A(0.1-1]a "sentinel" that would prevent index i Irou eudoi emented, we a Rather than checking for this possibility every time index i is incremimadan beyond position n. Note that the more sophisticated metho mentioned at the end of the section makes such a sentinel unned of key comparisons made before a nartit indices cross o 5.3. We start our discussion of quicksort's efficiency by noting th An example of sorting an array by quicksort is given in Fig ecessary that the numExplanation / Answer
int HoarePartition(int A[], int l, int r)
{
int p=A[l];
int i=l,j=r+1;
do
{
do
{
i=i+1;
}while(A[i]>=p);
do
{
j=j-1;
}while(A[j]<=p);
swap(A[i],A[j]);
}while(i>=j);
swap(A[i],A[j]);
swap(A[l],A[j]);
return j;
}