问答1 问答5 问答50 问答500 问答1000
网友互助专业问答平台

C#写的二分查找,有代码求指教

提问网友 发布时间:2022-04-23 14:20
声明:本网页内容为用户发布,旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:1656858193@qq.com
1个回答
热心网友 回答时间:2023-11-09 02:09
简单点评

严重点勿怪
种写
典型

1.
树立函数
概念



查找
肯定
让别
调用
函数


用户知道查找
数字位置

2.
检查参数
函数进
应该先检查参数
否合
叫防御式编程
数组


3.
函数体内
需要Console.WriteLine
语句
假设
类库函数
用户想
winform或者asp.net
调用
Console.WriteLine
何意义
候函数

值价值

4.
命名规范

随手写
请参考

查找(递归)
static
int
BinarySearch(int[]
array,
int
targetValue,
int
beginIndex,
int
endIndex)
{
if
(array
==
null)
{
throw
new
ArgumentNullException("...");
}
if
(beginIndex
<
0
||
endIndex
>
array.Length
-
1
||
beginIndex
>
endIndex)
{
throw
new
ArgumentException("...");
}
int
index
=
(beginIndex
+
endIndex)
/
2;
if
(targetValue
==
array[index])
{
return
index;
}
else
if
(targetValue
<
array[index])
{
return
BinarySearch(array,
targetValue,
beginIndex,
index
-
1);
}
else
{
return
BinarySearch(array,
targetValue,
index
+
1,
endIndex);
}
}

查找(非递归)
static
int
BinarySearch(int[]
array,
int
targetValue,
int
beginIndex,
int
endIndex)
{
if
(array
==
null)
{
throw
new
ArgumentNullException("...");
}
if
(beginIndex
<
0
||
endIndex
>
array.Length
-
1
||
beginIndex
>
endIndex)
{
throw
new
ArgumentException("...");
}
int
result
=
-1;
int
index
=
(beginIndex
+
endIndex)
/
2;
while
(beginIndex
<
endIndex)
{
if
(targetValue
==
array[index])
{
result
=
index;
break;
}
else
if
(targetValue
<
array[index])
{
endIndex
=
index
-
1;
}
else
{
beginIndex
=
index
+
1;
}
index
=
(beginIndex
+
endIndex)
/
2;
}
return
result;
}
热心网友 回答时间:2023-10-18 01:11
简单点评

严重点勿怪
种写
典型

1.
树立函数
概念



查找
肯定
让别
调用
函数


用户知道查找
数字位置

2.
检查参数
函数进
应该先检查参数
否合
叫防御式编程
数组


3.
函数体内
需要Console.WriteLine
语句
假设
类库函数
用户想
winform或者asp.net
调用
Console.WriteLine
何意义
候函数

值价值

4.
命名规范

随手写
请参考

查找(递归)
static
int
BinarySearch(int[]
array,
int
targetValue,
int
beginIndex,
int
endIndex)
{
if
(array
==
null)
{
throw
new
ArgumentNullException("...");
}
if
(beginIndex
<
0
||
endIndex
>
array.Length
-
1
||
beginIndex
>
endIndex)
{
throw
new
ArgumentException("...");
}
int
index
=
(beginIndex
+
endIndex)
/
2;
if
(targetValue
==
array[index])
{
return
index;
}
else
if
(targetValue
<
array[index])
{
return
BinarySearch(array,
targetValue,
beginIndex,
index
-
1);
}
else
{
return
BinarySearch(array,
targetValue,
index
+
1,
endIndex);
}
}

查找(非递归)
static
int
BinarySearch(int[]
array,
int
targetValue,
int
beginIndex,
int
endIndex)
{
if
(array
==
null)
{
throw
new
ArgumentNullException("...");
}
if
(beginIndex
<
0
||
endIndex
>
array.Length
-
1
||
beginIndex
>
endIndex)
{
throw
new
ArgumentException("...");
}
int
result
=
-1;
int
index
=
(beginIndex
+
endIndex)
/
2;
while
(beginIndex
<
endIndex)
{
if
(targetValue
==
array[index])
{
result
=
index;
break;
}
else
if
(targetValue
<
array[index])
{
endIndex
=
index
-
1;
}
else
{
beginIndex
=
index
+
1;
}
index
=
(beginIndex
+
endIndex)
/
2;
}
return
result;
}

本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。

吉祥人寿理财产品每年存1万连存10年15年后取出。这产品足真的吗? 在邮政储蓄银行投保吉祥人寿保险有无风险 我孩子住院做扁桃体手术,交了保险费在学校的,但他们说做手术不是意外事件不给报销,请问能报销吗?我... 港股的银行股估值这么低,为什么没有遭到外资的抢购 中国人寿保险的少儿教育险 港股H362122是哪家公司的? 吉祥人寿保险官方网站 港股100强,每年什么时候举行? A股、港股平均溢价率是多少? 小学生在学校交了保险,看病的钱可以报销吗? 投资恒生指数基金,但是没有港元,可以买恒生ETF(159920)吗? 吉祥人寿学生儿童保险与农村医疗挂钩?只报销能农村医疗保险未报销部分? 新人想在港股打新,A股和港股在这方面有什么区别吗?用老虎证券打新咋样... 学校续保换了保险公司学平险两家公司拒绝理赔? 2016年香港恒生指数在大陆到底怎么代理? 吉祥人寿保险公司学生保100元,重大疾病怎么陪? 港股真的创下近十年来新高吗? 什么法律,哪条哪款可以说明保险身故受益金转移不受外汇限制 身故受益人法定,保险赔款怎么分 被保人身故受益人是谁 我有个SQL in 使用参数传递怎么弄? 下面这段代码我要把JS数字随机更换写进复制里面该怎么写? 有一段用于Excel的代码不明白,请哪位大神可以帮我翻译一下这段话每一句的意思,谢谢!! html中的标签的target是做什么用的 怎么在BIOS里更改CPU风扇的转速啊? 求CSS3中target选择器的用法 进项税和销项税的计算公式分别是什么? 关于税收问题 比如购买钢筋,开发票怎么算税率 怎么算含税金额不能超出117000 为什么117000要除以1+17% 增值税计算 请教税收问题 怎样计算税率? 货款是10万 税率17% 那开票的话 总金额是117000 但是 117000中的 17000 增值税17%等于1.17,那1.17是怎么算来的。 &quot;炼乳”和“炼奶”有什么区别? 炼乳和牛奶哪个有营养 炼乳和炼奶有什么区别? 炼乳和牛奶的区别是什么?
Top