Question: Data Structures and Other Objects Using C++ Fourth Edition by Michael Main and Walter Savitch I am having trouble implementing this flexible quicksort function: void

Data Structures and Other Objects Using C++ Fourth Edition by Michael Main and Walter Savitch

I am having trouble implementing this flexible quicksort function:

 void quicksort( void* 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 type 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 Postcondition: The elements of the array have been rearranged so that they are in order from smallest to largest. 

In the code I as well need to implement this fucntion:

 int compare_ints(const void* ptr1, const void* ptr2) { // NOTE: *ptr1 is the first integer, but since ptr1 is a void // pointer, we must use the notation *((int *) ptr1) to refer to // this first integer. Similarly, *((int *) ptr2) refers to the // second integer. This comparison function works by subtracting // the second integer from the first, and returning the result. // This result meets the requirements listed above (for example, // it is negative when the second argument is larger than the // first. return *((int *) ptr1) - *((int *) ptr2); } 

We as well need to take into consideration that:

 int my_numbers[4000]; ...code to put 4000 numbers into the array my_numbers... 

Thank you in advance for your help :).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!