Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to develop quick sort using Hoare's partitioning. So it works on 100 unsorted data but 1000 data it's not working I guess. Here is my source code. I'd really really appreciate if you make it correct.

int partition( int a[], int low, int high )
{
	int piwot, right, left;
	left = low+1 ;
	right = high ;
	piwot = a[low];
	while(1)
	{
		while(a[right] > piwot) right--;
		while(a[left] < piwot && left < high) left++;
		if( left < right)
			swap(a, left, right);
		else{ 
			swap(a, low, right);
			return right;
		}
	}
}


void quickSort(int a[], int low, int high)
{
	int part;
	if(low < high)
	{
		part = partition(a, low, high);
		quickSort(a, low, part-1);
		quickSort(a, part+1, high);
	}
}
Posted
Updated 26-May-10 7:31am
v3

1 solution

It is easier to help if you give a description of in what way your code doesn't work, rather than just stating that it doesn't work.

However, I am guessing that your program appears to run for ever? I would suggest that you investigate what happens when a[right],a[left] and piwot are all the same value in your partition function.

Charles Keepax
 
Share this answer
 
Comments
T-Joe 27-May-10 1:53am    
Yeah ur right. it seems run for ever. Tkn u tkn u
Charles Keepax 27-May-10 2:49am    
Glad that helps, should be the only problem and it should work perfectly after you fix it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900