Member Avatar for TheFearful

So, I was given this specification for a quicksort algorithm and I have no idea where to start.
Create a template version of the QuickSort algorithm that will work with any data type.
Demonstrate the template with a drive function.

If it was to do a quicksort, I could easily do that, but I don't even know where to start. Any suggestions/tips? I did the rest of my project just fine, but is there a more efficient way I could do some of those things?

#include <iostream>
#include <algorithm>
#include <math.h>

using namespace std;

void pattern(unsigned int n);
void super_write_vertical(int number);
void Npattern(int m);

int main()
{
    const int SIZE = 10; //Array Size
    unsigned int n;
    int number;
    int m;
    int qs[SIZE] = {7, 3, 9, 2, 0, 1, 8, 4, 6, 5};

    n = 840;
    cout << "For Item #1";
    cout << "Pattern " << n << ":" << endl;
    pattern(n);

    n = 4000;
    cout << "Pattern " << n << ":" << endl;
    pattern(n);

    number = -361;
    cout << "\nFor Item #2";
    cout << "Super Write Vertical " << number << ":" << endl;
    super_write_vertical(number);

    number = -1234;
    cout << "Super Write Vertical " << number << ":" << endl;
    super_write_vertical(number);

    number = 1234;
    cout << "Test Case to prove third case without negatives:" << endl;
    super_write_vertical(number);

    m = 3;
    cout << "\nFor Item #3" << endl;
    cout << "Exponent " << m << ":" <<endl;
    Npattern(m);

    m = 4;
    cout << "\nExponent " << m << ":" << endl;
    Npattern(m);
    cout << endl;

    cout << "\nFor QuickSort" << endl;
    for(int i = 0; i < SIZE; i++)
    {
        cout << qs[i] << " ";
    }
    cout << endl;

    for(int i = 0; i < SIZE; i++)
    {
        cout << qs[i] << " ";
    }
    cout << endl;

    return 0;
}

void pattern(unsigned int n)
{
    if(n <= 8484)
    {
        cout << n << endl; //Will print out the values of n
        pattern(n * 2); 
        cout << n << endl; //This one will print out the values of n that were being stored in the stack
    }
    return;
}

void super_write_vertical(int number)
{
    if(number < 0)
    {
        cout << "-" << endl;
        super_write_vertical(abs(number)); //Will change that negative number to positive using the absolute value
    }
    else if(number < 10)
    {
        cout << number << endl; //will print out the only number that was given if there was one
    }
    else
    {
        super_write_vertical(number / 10); //This will print out everything but the last digit
        cout << number % 10 << endl; //This will print out the very last digit
    }
}

void Npattern(int m)
{ 
    if(m == 0)
    {
        return;
    }
    Npattern(m-1);
    cout << m << " ";
    Npattern(m-1);
}

Too much code, and not enough information. What is the "drive" function supposed to do, for example? What do you mean by a "recursive" quick sort? Do you mean a reverse sort - largest to smallest? Or do you mean a recursive sorting algorithm?

Member Avatar for TheFearful

Sorry for the confusion.
For the quick sort, that was all the information I was given. I was told to make a template version of the algorithm using recursion where it can work with any data type. I have only ever done a simple template before and am looking for guidance on how to solve this. I enjoy when I am able to solve something.

Member Avatar for TheFearful

Oh and it's smallest to largest. So if I had 3846710592, it would be 0 1 2 3 4 5 6 7 8 9
I can do a regular quicksort to do that just fine, but I am confused with the Template and drive function as I do not know what it is I am being asked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.