主题:return 跟不return??为什么会这样子,递归??
9.26② 试将折半查找算法改写成递归算法。
实现下列函数:
int BinSearch(SSTable s, int low, int high, KeyType k);
/* Index the element which key is k */
/* in StaticSearchTable s. */
/* Return 0 if x is not found. */
静态查找表的类型SSTable定义如下:
typedef struct {
KeyType key;
... ... // 其他数据域
} ElemType;
typedef struct {
ElemType *elem;
int length;
} SSTable
int BinSearch(SSTable s, int low, int high, KeyType k)
/* Index the element which key is k */
/* in StaticSearchTable s. */
/* Return 0 if x is not found. */
{
int mid;
if (low>high) return ERROR;
mid=(low+high)/2;
if (k==s.elem[mid].key) return mid;
else if (k<s.elem[mid].key)
{high=mid-1;
return BinSearch(s,low,high,k);}
else if (k>s.elem[mid].key)
{low=mid+1;
return BinSearch(s,low,high,k);}
}
请问一下,如果我改成这样子就不对
int BinSearch(SSTable s, int low, int high, KeyType k)
/* Index the element which key is k */
/* in StaticSearchTable s. */
/* Return 0 if x is not found. */
{
int mid;
if (low>high) return ERROR;
mid=(low+high)/2;
if (k==s.elem[mid].key) return mid;
else if (k<s.elem[mid].key)
{high=mid-1;
BinSearch(s,low,high,k);}
else if (k>s.elem[mid].key)
{low=mid+1;
BinSearch(s,low,high,k);}
}
E.WAY(12384610) 19:34:04
不加return 就不对,为什么呢??
E.WAY(12384610) 19:34:52
不知有谁知道,说一下给我听怎么样
可不可以用栈来说明一下呢?
实现下列函数:
int BinSearch(SSTable s, int low, int high, KeyType k);
/* Index the element which key is k */
/* in StaticSearchTable s. */
/* Return 0 if x is not found. */
静态查找表的类型SSTable定义如下:
typedef struct {
KeyType key;
... ... // 其他数据域
} ElemType;
typedef struct {
ElemType *elem;
int length;
} SSTable
int BinSearch(SSTable s, int low, int high, KeyType k)
/* Index the element which key is k */
/* in StaticSearchTable s. */
/* Return 0 if x is not found. */
{
int mid;
if (low>high) return ERROR;
mid=(low+high)/2;
if (k==s.elem[mid].key) return mid;
else if (k<s.elem[mid].key)
{high=mid-1;
return BinSearch(s,low,high,k);}
else if (k>s.elem[mid].key)
{low=mid+1;
return BinSearch(s,low,high,k);}
}
请问一下,如果我改成这样子就不对
int BinSearch(SSTable s, int low, int high, KeyType k)
/* Index the element which key is k */
/* in StaticSearchTable s. */
/* Return 0 if x is not found. */
{
int mid;
if (low>high) return ERROR;
mid=(low+high)/2;
if (k==s.elem[mid].key) return mid;
else if (k<s.elem[mid].key)
{high=mid-1;
BinSearch(s,low,high,k);}
else if (k>s.elem[mid].key)
{low=mid+1;
BinSearch(s,low,high,k);}
}
E.WAY(12384610) 19:34:04
不加return 就不对,为什么呢??
E.WAY(12384610) 19:34:52
不知有谁知道,说一下给我听怎么样
可不可以用栈来说明一下呢?