Question

In: Computer Science

implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std;...

implement c++ Quicksort using median of 3

#ifndef QSORT_H

#define QSORT_H

#include

#include

using namespace std;

template

T median_of_three(T& a, T& b, T& c, TComparator comparator) {

}

template

size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) {

// TODO: implement.

}

template

void QuickSort(vector& vec, TComparator comparator,size_t low,size_t high) {

if(comparator(low,high)){

size_t loc = partition(vec,comparator,low,high);

QuickSort(vec,comparator,low,loc-1);

QuickSort(vec,comparator,loc+1,high);

}

return;

}

template

void quicksort(vector& vec, TComparator comparator) {

// TODO: implement.

size_t size = vec.size();

QuickSort(vec,comparator,0,size-1);

}

#endif

test_case:

int main() {

srand(time(NULL));

vector<int> test;

for (int i =0; i < 10; i++) {

test.push_back(rand() % 1000);

}

for (int i = 0; i < 10; i++) {

cout << test[i] << " ";

}

cout<<"Before sort;"<<endl;

quicksort(test, std::less<int> {});

for (int i = 0; i < 10; i++) {

cout << test[i] << " ";

}

cout << endl;

return 0;

}

Solutions

Expert Solution

#include"stdafx.h"
#include <iostream>
#include<algorithm>
using namespace std;
#define size 10
int i;                               
void show(int* array, int n);
int partition(int* array, int pValue, int left, int right);
void QuickSort(int* array, int left, int right);

int main(void)
{
    int array[size];
    int i;

    for( i = 0; i < size; i++)              
    {
         array[i]=rand()%100;
    }

    cout<<endl<<"The random generated numbers are: "<<endl;
    show(array, size);
    QuickSort(array,0,size - 1);                
    cout<<endl<<"The sorted numbers are : "<<endl;
    show(array, size);

    system("pause");
    return 0;
}

void show(int* array, int n)
{
    int i;

    for( i = 0; i < n; i++) cout<<array[i]<<'\t';
}



void QuickSort(int* array, int left, int right)
{
    for(i=0;i<3;i++)
    {
       array[i]=array[rand()%100];
      }
      stable_sort(array,array+3);
     int p=array[(i+1)/2];
    //int p = array[left];              
    int split;

    if(right > left)                         
    {
        split = partition(array, p, left, right);

        array[split] = p;
        QuickSort(array, left, split-1);   
        QuickSort(array, split+1, right);    
    }
}


int partition(int* array, int p, int left, int right)
{
    int lb = left;
    int rb = right;

    while(lb < rb)             
    {
         while( p < array[rb]&& rb > lb)      
         {
              rb--;                     
         }
         swap(array[lb], array[rb]);

         while( p >= array[lb]&& lb < rb)     
         {
              lb++;                      
         }
         swap(array[rb], array[lb]);

    }
    return lb;                            


}

Related Solutions

implement c++ Quicksort using median of 3 as pivot. this also pass comparator as a function...
implement c++ Quicksort using median of 3 as pivot. this also pass comparator as a function param so it can sort vector in increasing or decreasing order based on the comparator passed. the test code uses std::less comparator. #ifndef QSORT_H #define QSORT_H #include #include using namespace std; template T median_of_three(T& a, T& b, T& c, TComparator comparator) { } template size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) { // TODO: implement. } template void QuickSort(vector& vec, TComparator comparator,size_t...
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std;...
Implement the following functions for an array based stack. #include <stdexcept> #include <iostream> using namespace std; #include "Stack.h" template <typename DataType> class StackArray : public Stack<DataType> { public: StackArray(int maxNumber = Stack<DataType>::MAX_STACK_SIZE); StackArray(const StackArray& other); StackArray& operator=(const StackArray& other); ~StackArray(); void push(const DataType& newDataItem) throw (logic_error); DataType pop() throw (logic_error); void clear(); bool isEmpty() const; bool isFull() const; void showStructure() const; private: int maxSize; int top; DataType* dataItems; }; //-------------------------------------------------------------------- // // // ** Array implementation of the Stack ADT...
#ifndef CONTAINER_H #define CONTAINER_H using namespace std; class container { public: // CONSTRUCTOR container(); ~container(); container(const...
#ifndef CONTAINER_H #define CONTAINER_H using namespace std; class container { public: // CONSTRUCTOR container(); ~container(); container(const container& c); container& operator=(const container& c) // MEMBER FUNCTIONS bool lookup(const int& target); void remove(const int& target); void add(const int& target); private: struct node{ int key; node *next; }; node* head; node* tail; }; #endif For a linked list based implementation of a container class shown above, implement the constructor, copy constructor, destructor and copy assignment functions.
#include #include using namespace std; //Implement function lookUpFirstNeg() here int main() { double *dd, *ddAns; //allocate...
#include #include using namespace std; //Implement function lookUpFirstNeg() here int main() { double *dd, *ddAns; //allocate memory for at least 3 double elements //ensure all elements have been initialized //call function, replace 0 with the proper number of elements pointed to by dd ddAns = lookUpFirstNeg(dd, 0); if(ddAns == NULL) cout << "No negatives found.\n"; else cout << "First negative value is:" << ddAns << endl; //properly deallocate memory allocated above return 0; }
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell {...
Complete the C++ code #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; struct Cell { int val; Cell *next; }; int main() { int MAX = 10; Cell *c = NULL; Cell *HEAD = NULL; srand (time(NULL)); for (int i=0; i<MAX; i++) { // Use dynamic memory allocation to create a new Cell then initialize the // cell value (val) to rand(). Set the next pointer to the HEAD and // then update HEAD. } print_cells(HEAD); }
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; //...
Write a C++ program based on the cpp file below ++++++++++++++++++++++++++++++++++++ #include using namespace std; // PLEASE PUT YOUR FUNCTIONS BELOW THIS LINE // END OF FUNCTIONS void printArray(int array[], int count) {    cout << endl << "--------------------" << endl;    for(int i=1; i<=count; i++)    {        if(i % 3 == 0)        cout << " " << array[i-1] << endl;        else        cout << " " << array[i-1];    }    cout...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
Writing an nth root program in C++ using only: #include using namespace std; The program must...
Writing an nth root program in C++ using only: #include using namespace std; The program must be very basic. Please don't use math sqrt or pow . For example, the 4th root of 16 is 2 because 2 * 2 * 2 * 2 = 16. The 4th root of 20 is 2 because 2 * 2 * 2 * 2 = 16 and 16 is less than 20, and 3 * 3 * 3 * 3 = 81, which...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; //...
In C++, assuming you have the following incomplete code: #include<iostream> #include <unistd.h> using namespace std; // Structure for storing the process data struct procData { char pname[5]; // Name of a process int arrivt; //Arrival time of a process int pburst; // Burst time of a process int endtime; // Exit time/ Leaving time of a process int remburst; // Remaining burst time of a process int readyFlag; // boolean, Flag for maintaining the process status }; // Global variable...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT