Converting Quick sort to template

My whole code is included below, although what is really important to me is only the first two functions, the quicksort and the partition...
Whenever I build this program, it keeps returning an error for the calling of the partition function, citing that the first variable is in an improper format... I don't know why it's not working though... Can anybody help?


Whole Code:
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

template <class T>
void quicksort(T arr[], int start, int end) {
if (start < end) //test for base case start == end
{
// Partition the array and get the pivot point.
int p = partition(arr, start, end);

// Sort the portion before the pivot point.
quicksort(arr, start, p - 1);

// Sort the portion after the pivot point.
quicksort(arr, p + 1, end);
}
return;
}

template <class T>
int partition(T arr[], int start, int end)
{
// The pivot element is taken to be the element at
// the start of the subrange to be partitioned.
T pivotValue = arr[start];
int pivotPosition = start;

// Rearrange the rest of the array elements to
// partition the subrange from start to end.
for (int pos = start + 1; pos <= end; pos++)
{
if (arr[pos] < pivotValue)
{
// arr[pos] is the "current" item.
// Swap the current item with the item to the
// right of the pivot element.
swap(arr[pivotPosition + 1], arr[pos]);
// Swap the current item with the pivot element.
swap(arr[pivotPosition], arr[pivotPosition + 1]);
// Adjust the pivot position so it stays with the
// pivot element.
pivotPosition ++;
}
}
return pivotPosition;
}

template <class T>
void print(T arr[], int size){
cout << "These are the members of the array: \n";
for (int i = 0; i < size; i++) {
cout << arr[i] << endl;
}
return;
}

int main()
{
int arrayInt[50];
string arrayString[50];
char arrayChar[50];
double arrayDouble[50];
int choice, size;
cout << "\t\tPress 1 to enter in an array of integers.\n";
cout << "\t\tPress 2 to enter in an array of strings.\n";
cout << "\t\tPress 3 to enter in an array of characters.\n";
cout << "\t\tPress 4 to enter in an array of doubles.\n";
cin >> choice;
cout << "Please enter how many pieces of data you want to input: ";
cin >> size;
switch (choice) {
case 1:
for (int i = 0; i < size; i++)
cin >> arrayInt[i];
quicksort(arrayInt, 0, size-1);
print(arrayInt, size - 1);
break;
case 2:
for (int i = 0; i < size; i++)
cin >> arrayString[i];
quicksort(arrayString, 0, size-1);
print(arrayString, size);
break;
case 3:
for (int i = 0; i < size; i++)
cin >> arrayChar[i];
quicksort(arrayChar, 0, size-1);
print(arrayChar, size);
break;
case 4:
for (int i = 0; i < size; i++)
cin >> arrayDouble[i];
quicksort(arrayDouble, 0, size - 1);
print(arrayDouble, size);
break;
default:
cout << "\nPlease enter a choice.\n";
}
return 0;
}
Please use code tags: http://www.cplusplus.com/forum/articles/42672/

Move your partition template ahead of the quicksort template. With the order that have them the compiler is using std::partition in quicksort because your version of partition is not yet defined.
Thanks. Much appreciated. It works now.
closed account (o1vk4iN6)
Not sure if you are just doing this for fun or whatever, but C++ already has a templated sort function (not sure if it is defined to be quick sort but it is O(n logn)).

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <algorithm>

int main()
{
    int arr[] = { 4, 1, 5, 133, 22, 10, 18 };

    std::sort(arr, arr+7);

    for( int v : arr ) std::cout << v << " ";

    return 0;
}


Also a good example of why you don't want to do "using namespace std;" :P.
Last edited on
Thanks. I'm taking an advanced programming class (second class in my comp. sci major) and one of the extra credit assignments were to convert the quicksort function to a template. My professor was pretty clear on using the using namespace std header. We haven't learned any other way yet anyway. Thanks for the help though. Much appreciated.
closed account (o1vk4iN6)
Your prof is promoting the use of "using namespace std" ? The class I had on C++, day one our prof specifically told us not to use it. It isn't that hard, it is just a namespace and you access it by using a double colon :: so everything is hidden and you don't have name clashes like you did with partition. You just add "std::" in front anything you want to use that is in the standard library.
I'll need to look into that. But right now, I'd like to stick to my professor's instructions because I'm not a very experimental person right now. I'm trying to focus on finals. Thanks for the tip though. Will definitely look into that.
Topic archived. No new replies allowed.