【发布时间】:2014-04-16 22:30:08
【问题描述】:
我是编程新手,我正在学习 C。我正在尝试通过使用递归来解决问题。我找到了很多关于这方面的信息,我可以在我的程序中使用它,但我仍然想尝试一些不同的东西。我的问题如下:我有
bool search(int value, int values[], int n)
// int value is value to search,
// int values[] is the array in which value is to be found (or not)
// int n is size of array
// some code here and then:
if (middle_number > value)
{
int new_array[] = values[0:middle_index];
// I want my new array to be some slice of values[]
// by declaring a range from 0 to the middle_index
// Is that possible?
search(value, new_array, middle_index);
// Using recursion
}
我可以循环创建新数组,但是我认为我会失去二分搜索的优势(更好的性能)
【问题讨论】:
-
那么问题是什么?
-
我可以实现吗? new_array[] = values[0:middle_index] 不使用循环?
-
只在同一个阵列上工作会获得更好的性能。拆分数组时,只需将数组变量以及要处理的范围的开始和结束索引传递给递归函数。无需复制数组或其任何子集。