hello all,

I am writing a program to test sorting times, and I am having trouble with my quick sort. essentially, my quicksort is not sorting and I am having alot of trouble with it. Does anyone see any real problems with my implementation?

Thanks!

//--------------------------------------------------------
void quicksort(name_t *&charArray, int left, int right)
{
	if (left < right)
	{
		int pivot = partition(charArray, left, right);
		quicksort(charArray, left, pivot-1);
		quicksort(charArray, pivot+1, right);
	}
}
//--------------------------------------------------------
int findpivot (name_t *&charArray, int left, int right)
{return (left+right)/2;}
//--------------------------------------------------------

int partition(name_t *&charArray, int left, int right)
{
	int pivot = findpivot(charArray,left,right);
	char temp[31];
	while (left < right)
	{
		while (charArray[left] < charArray[pivot]) left++;
		while (charArray[right] > charArray[pivot]) right--;
		if (left < right)
		{
			strcpy(temp,charArray[right]); //right
			strcpy(charArray[right],charArray[left]);//right left
			strcpy(charArray[left],temp);//left
		}
		else
		{
			return right;
		}
	}
}

1. Don't exactly know how name_t in your code is defined but I don't really see the purpose of using *& before charArray. I bet you are confused with passing array as a function parameter. Have a look into it.
2. If your charArray is really and array of strings rather than array of characters then you cannot compare two strings with < , you actually need to use strcmp function or your own comparison function.
3. You can reduce time by taking the pivot initially as first element and not calling find pivot function. As with a slightly different implementation the return value of the pivot would be properly adjusted at the end.
4. Have a look at this once properly.

In addition to csurfer's points, if the code compiles and runs but return the same un-sorted array you need to check how the quicksort() is called. Specifically the *& which is a reference/de-reference. Make sure the array is passed in as a pointer.

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.