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
  • The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function...

    The function retrieveAt of the class arrayListType is written as a void function. Rewrite this function so that it is written as a value returning function, returning the required item. If the location of the item to be returned is out of range, use the assert function to terminate the program. note: please give all code in c++ below is the class and implementation(a test program would be helpful in determining how to use it): class arrayListType { public:    ...

  • quickSort function. Calling previous functions already implemented. This is a bit different type of quicksort function...

    quickSort function. Calling previous functions already implemented. This is a bit different type of quicksort function where my partition function has 3 parameters instead of 4. So I need to write a function quicksort that actually puts all 3 of my functions together and finalizes the sorting process "There should be almost nothing done besides calling the other 3 functions and recursively calling itself (except to check for basecase) class quicksort { private: int left; int right; int* array;   ...

  • Create qz5.c to include all of the following function prototypes: void check1(char *, char, int *);...

    Create qz5.c to include all of the following function prototypes: void check1(char *, char, int *); void check2(char *, char, int *); void display(char, int); Then, implement main() to perform the tasks below: Define a 10-element char array with initial values of any lower case letters of your selection. Values can duplicate. Define a pointer that points to the above array. Print the array completely with double spaces before each character. See screenshot below for a sample. Call check1() with...

  • (C++) Two stacks of the same type are the same if they have the same number...

    (C++) Two stacks of the same type are the same if they have the same number of elements and their elements at the corresponding positions are the same. Overload the relational operator == for the class stackType that returns true if two stacks of the same type are the same; it returns false otherwise. Also, write the definition of the function template to overload this operator. Write a program to test the various overloaded operators and functions of classstackType. **Please...

  • I have to a C++ program I need help with. Can you break it into the...

    I have to a C++ program I need help with. Can you break it into the two steps provided below. Suppose MAX_SIZE is a global constant int variable that has been declared and initialized to an appropriate positive value. 1. int maxValue(const array<int, MAX_SIZE>&, int); is the prototype for a function that returns the value of the largest element in the array parameter. The array may be only partially filled. The int parameter indicates how many items are actually in...

  • C++ Write a function SwapArrayEnds() that swaps the first and last elements of the function's array...

    C++ Write a function SwapArrayEnds() that swaps the first and last elements of the function's array parameter. Ex: sortArray = {10, 20, 30, 40} becomes {40, 20, 30, 10}. The array's size may differ from 4. #include <iostream> using namespace std; /* Your solution goes here */ int main() {    const int SORT_ARR_SIZE = 4;    int sortArray[SORT_ARR_SIZE];    int i = 0;    sortArray[0] = 10;    sortArray[1] = 20;    sortArray[2] = 30;    sortArray[3] = 40;...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element...

    (C++ program )Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to the element 1 of the new array. Element 1 of the argument array should be copied to element 2 of the new array, and so forth....

  • C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble...

    C++. Difficulty with quickSort function. Code will not run quickSort function. The code I'm having trouble with is in bold. -------------------------------------------------------------------------------------------------driverProgram.cpp #include #include #include #include #include "quickSort.cpp" using namespace std; int main() { const int MIN_SIZE = 4; //Array size const int SIZE = 25; int theArray[SIZE] = {11, 22, 33, 44, 55, 66, 77, 88, 99, 12, 13, 14, 15, 16, 17, 18, 19, 18, 19, 20, 21, 22, 23, 24, 25}; cout << "List of 25 items: ";...

  • 1. Write a statement that calls a function named showSquare, passing the value 10 as an...

    1. Write a statement that calls a function named showSquare, passing the value 10 as an argument. 2. Write the function prototype for a function called showSquare. The function should have a single parameter variable of the int data type and areturn type of void. 3. Write the function prototype for a function called showScoreswith a parameter list containing four integer variables and a return type of void. 4. Look at the following function definition: double getGrossPay(int hoursWorked, double payRate)...

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