Question
C++
The Functions Specification You will be writing a function with this specification: 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 This specification includes several new items that you havent 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).
media%2Fba6%2Fba66de15-a48b-4f0e-8da4-cd
media%2F4f4%2F4f4d7aea-e372-40cb-93e5-e9
media%2F491%2F491f8541-3f5d-4846-8d07-7d
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. & 3. QuickSort Function (Without Recursion)

// An iterative implementation of quick sort
#include <stdio.h>

// x utility function to turnIn two elements
void turnIn ( int* x, int* y )
{
   int t = *x;
   *x = *y;
   *y = t;
}

/* This function is same in both iterative and recursive*/
int divide (int array[], int p, int m)
{
   int x = array[m];
   int oo = (p - 1);

   for (int qq = p; qq <= m- 1; qq++)
   {
       if (array[qq] <= x)
       {
           oo++;
           turnIn (&array[oo], &array[qq]);
       }
   }
   turnIn (&array[oo + 1], &array[m]);
   return (oo + 1);
}

void SortIteration (int array[], int p, int m)
{
   // Create an auxiliary stack
   int stcock[ m - p + 1 ];

   // Initialize the top of the stack
   int tos = -1;

   // Push initial values to the stack
   stcock[ ++tos ] = p;
   stcock[ ++tos ] = m;

   // Keep popping the stack
   while ( tos >= 0 )
   {
       // Pop m and p
       m = stcock[ tos-- ];
       p = stcock[ tos-- ];

       //See the correct position of array in the right
       int p = divide( array, p, m );

       //If there are elements on left push the stack to left
       if ( p-1 > p )
       {
           stcock[ ++tos ] = p;
           stcock[ ++tos ] = p - 1;
       }

       //If there are elements on right push the stack to right
       if ( p+1 < m )
       {
           stcock[ ++tos ] = p + 1;
           stcock[ ++tos ] = m;
       }
   }
}

// Print contents of the array
void displayArray( int array[], int ll )
{
   int oo;
   for ( oo = 0; oo < ll; ++oo )
       printf( "%d ", array[oo] );
}

// Main to test above Program
int main()
{
   int array[] = {4, 3, 5, 2, 1, 3, 2, 3};
   int ll = sizeof( array ) / sizeof( *array );
   SortIteration( array, 0, ll - 1 );
   displayArray( array, ll );
   return 0;
}

Output:

for (int qq p; qq < m 1; qq++) 19 20 ▼ 21 if (array[qq] <-x) 23 24 25 26 00++ turnIn (&array [oo], &array[qq]); Upload Progra

2. Insertion Sort in Array


#include <stdio.h>

int main()
{
int aa, arr[1000], ll, ff, pp;   // declaring variables

printf("Enter elements\n");
scanf("%ff", &aa);

printf("Enter %ff integers\n", aa);

for (ll = 0; ll < aa; ll++) { // Loop for initializing array
scanf("%ff", &arr[ll]);
}

for (ll = 1 ; ll <= aa - 1; ll++) {
ff = ll;

while ( ff > 0 && arr[ff] < arr[ff-1]) {
pp = arr[ff];
arr[ff] = arr[ff-1];                   //loop for sorting using insertion sort
arr[ff-1] = pp;

ff--;
}
}

printf("The Sorting using Insertion Sorting is:\n");

for (ll = 0; ll <= aa - 1; ll++) {           // printing the result
printf("%ff\aa", arr[ll]);
}

return 0;
}

Output:

Enter number of elements 5 Enter 5 integers 5 6 Sorted list in ascending order: 5

Please rate the answer if it helped. Thankyou.

Hope it helps...

Add a comment
Know the answer?
Add Answer to:
C++ The Function's Specification You will be writing a function with this specification: void quicksort void*...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT