方法在不同函数中的变化值。 [英] value of method changing in different functions.

查看:52
本文介绍了方法在不同函数中的变化值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





i有以下代码;



Hi,

i have the code below;

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);
	}
}







它值为pivotpoint作为j,

但当它转回快速排序功能时,枢轴点的值为钢零,而阵列按编程更改。



为什么它不会更改为新值?



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?

推荐答案

您正在将 pivotpoint 传递给 partition 作为值而不是引用... http://www.learncpp.com/cpp-tutorial/73-passing-arguments-by-reference/ [ ^ ]
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/[^]


这篇关于方法在不同函数中的变化值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆