【发布时间】:2013-10-24 03:25:03
【问题描述】:
我正在编写一个递归二进制搜索程序。这是我到目前为止所得到的。该程序的参数是它包含 2 个函数,主函数和第二个函数,它将对传递的值进行二进制排序。该程序可以工作,但它不会递归搜索函数,我认为它不使用二进制搜索...
/* ex06_18.c */
#include <stdio.h>
#define SIZE 10
/* function prototype */
void someFunction( const int b[], int startIndex, int size );
/* function main begins program execution */
int main( void )
{
int a[ SIZE ] = { 8, 3, 1, 2, 6, 0, 9, 7, 4, 5 }; /* initialize a */
printf( "Answer is:\n" );
someFunction( a, 0, SIZE );
printf( "\n" );
return 0; /* indicates successful termination */
}
void someFunction( const int b[], int startIndex, int size )
{
if ( startIndex < size ) {
someFunction( b, startIndex + 1, size );
printf( "%d ", b[ startIndex ] );
} /* end if */
} /* end function someFunction */
【问题讨论】:
-
“程序有效”——除非你对“搜索”的概念与其他人完全不同。 “它不递归搜索函数”——只是因为它不搜索;显然,它是递归的。 “我不认为它使用二进制搜索”——你不这么认为吗?您是否跳过课堂讨论,没有阅读有关该主题的教科书?即使是这样,也可以通过 google 获得大量参考资料。 “二元排序”——等等,现在你要排序吗?排序、搜索和打印所有值是三件不同的事情。