Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You will be writing a function with this specification: void quicksort (voids *

ID: 3841964 • Letter: Y

Question

You will be writing a function with this specification: void quicksort (voids * base, size_t n, size_t bytes, int compar (const void*, const void*)) precondition base is a pointer to the first component of an array with at least n elements The component of the array may be any type at all, and the parameter bytes must be the number of bytes in each component of the array. The fourth parameter compar, must be the name of a function that can compare two elements of the array. The two arguments of compar are pointers to two elements in the array, and the return value of compar indicates which of the two arguments is largest, as follows: a negative return value means that the 2nd argument is larger a zero return value indicates that the arguments are equal a positive return value means that the 1st argument is larger Post condition: The elements of the array have been rearranged so that they are in order from smallest to largest This specification includes several new items that you haven't seen before, such as the third parameter (which must be the number of bytes required by one component of an array) and the fourth parameter (where the actual argument must be a function that you write and make available to the quicksort function).

Explanation / Answer

The function is:
void quicksort(
   void* base;
   size_t n;
   size_t bytes,
   int compar(const void*,const void*)
)

int compar(const void* a, const void* b)
{
   if(b>a)
       return -1;
   else if(a>b)
       return 1;
   else
       return 0;
}

void *base=arr;   //arr is the name of the array,Globally defined
   void *end=base+n;

void quicksort(void* base,void* end)
{
  
   if(base<end)
   {
       par = partition();
       quicksort(base,par-1);
       quicksort(par+1,end)
   }

}

int partition(void* base, void* end)
{
   void* pi = *end;
   int i = (base - 1);

   for(int j = 0; j<=(end-start) ; j++)
   {
       if(compar(base+j , pi)!=-1)
       {
           i++;
           swap(base+i , base+j);
       }
   }
   swap(base+i+1 , end);
   return i+1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote