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:
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:
Please rate the answer if it helped. Thankyou.
Hope it helps...
C++ The Function's Specification You will be writing a function with this specification: void quicksort void*...
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: ...
(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...
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; ...
1. Here are codes to define a stack class based on dynamic array, please complete the copy constructor //--- Definition of Stack copy constructor Stack::Stack(const Stack & original) : myCapacity(original.myCapacity), myTop(original.myTop) { //--- Get new array for copy myArray = new(nothrow) StackElement[myCapacity]; if (myArray != 0) // check if memory available // copy original's array member into this new array { // Please complete the function here } else { cerr << "*Inadequate memory to allocate...
Please Help This: please follow style guidelines Rule 1: use const where appropriate Rule 2: every member function must include a description in the header file including precondition and postcondition. Rule 3: every member variable must include a description of what the variable holds. Rule 4: Classes should be capitalized. Bag, not bag. Person, not person. (I know this is different than how the book does it) Rule 5: member variables of classes should be preceded by “m_”. If it’s...
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 -------------------------------------------------------------- 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()...
Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...
(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 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: ";...