Trying to make a quicksort for a templated array

template <class DataType>
void Array<DataType>::sort(int left, int right)
{
int i = left, j = right;
DataType temp;
DataType pivot = (*this)[(left + right) / 2];
while (i <= j)
{
while ((*this)[i] < pivot)
i++;
while ((*this)[j] > pivot)
j--;
if (i <= j)
{
temp = (*this)[i];
(*this)[i] = (*this)[j];
(*this)[j] = temp;
i++;
j--;
}
}
if (left < j)
sort(left, j);
if (i < right)
sort(i, right);
}

here's my quicksort, problem is it only seems to work with integers, I'm trying to make this work for a templated array with an object with ONLY the less-than overloaded operator programmed in. Any suggestions as to how to get this to work would be appreciated
More important, for which type do you think it doesn't work?

an object with ONLY the less-than overloaded operator programmed in

Then you shouldn't use greater than.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct SubjectCode
{
	string name;
	int count;
	bool operator==(const SubjectCode& a) const
	{
		if(this->name == a.name)
			return true;
		else
			return false;
	}
	bool operator<(const SubjectCode& right) const
	{
		return this->name < right.name;
	}
};


Here ya go, trying to use it to sort an array of this type, but it calls a compiler error
I get if I were to overload an operator> I'd fix it, but I'm trying to get it to not require an operator>. Any suggestions would be helpful. In other words this would require a change of the sort function and not of the object itself.

@Athar: "Then you shouldn't use greater than." That's why I'm asking for help! Trying to fix the sort function so I don't use it!
Last edited on
That's not very specific. What kind of runtime error and in which line does it happen?

Also, constructs like this are completely unnecessary:
1
2
3
4
if(this->name == a.name)
			return true;
		else
			return false;


Just write: return name==a.name;
@Athar: "That's not very specific. What kind of runtime error and in which line does it happen?

Also, constructs like this are completely unnecessary:
1
2
3
4
if(this->name == a.name)
return true;
else
return false;


Just write: return name==a.name;"



Yes, yes I get that, though the 2 things actually do the exact same thing, that's why I'm posting in beginner forums.

here:
struct SubjectCode
{
string name;
int count;
bool operator==(const SubjectCode& a) const
{
return this->name == a.name;
}
bool operator<(const SubjectCode& right) const
{
return this->name < right.name;
}
};

fixed, still looking for help with the quicksort function and NOT with the object.
Last edited on
So, about that runtime error? Saw the edit.
Also, you should edit your first post and add code tags. The code is unreadable otherwise.

That's why I'm asking for help! Trying to fix the sort function so I don't use it!

Ah, well. a>b is the same as b<a.
Last edited on
@Athar: It's a quicksort code in a templated array.
All I really need is pseudocode or an algorithm for a generic quicksort function that doesn't require a "greater than" while statement or separate "pivot" or "swap" functions. I've tried googling generic quicksort functions but it's been rather unhelpful. Or better yet simply a way to bypass a "greater than" while statement
Last edited on
Yeah, see my edit.
and.....I'm an idiot lol, I tried it before but it kept throwing errors, now it's working, must've typo'd it or something. Thanks for dealing with me ^_^
Last edited on
Topic archived. No new replies allowed.