Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i have the code below;

C++
void exchange(int i, int j, int arr[])
{
	int k=arr[i];
	arr[i] = arr[j];
	arr[j] = k;
}


void partition(int low, int high, int pivotpoint, int arr[],int arraysize)
{
	int i=0, j=0, pivotitem = arr[low];	
	
	j=low;
	for(i=low+1;i<=high;i++)
	{		
		if(arr[i] < pivotitem)
		{					
			j++;
			exchange (i,j,arr);					
		}		
	}
	pivotpoint = j;
	exchange(low,pivotpoint,arr);
}


void quicksort(int low, int high,int arr[],int arraysize, int pivotpoint)
{
	if(high > low)
	{
		partition(low, high, pivotpoint, arr,arraysize);
		quicksort(low,pivotpoint-1,arr,arraysize,pivotpoint);
		quicksort(pivotpoint+1,high,arr,arraysize,pivotpoint);
	}
}


when partition function job ends,
it values pivotpoint as j,
But when it turns back to quicksort function, the value of pivotpoint is steel zero, while the array is changed as programmed.

why doesn't it change to the new value?
Posted

1 solution

You are passing pivotpoint into the partition function as a value instead of a reference ... http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/[^]
 
Share this answer
 
Comments
m.r.m.40 31-May-13 13:07pm    
Thank you!
Sergey Alexandrovich Kryukov 31-May-13 22:49pm    
Good catch, a 5.
—SA

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