会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
易懂
简单就是好!
博客园
首页
新随笔
联系
管理
订阅
C#数组二分查找法
看了一下的二分查找法!就用C#写了一下代码。
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
ConsoleApplication1
{
class
array
{
public
static
void
Main(
string
[] args)
{
Data data
=
new
Data(
10
);
data.add(
1
);
data.add(
32
);
data.add(
12
);
data.add(
45
);
data.add(
24
);
data.cancel(
12
);
data.size();
data.display();
//
data.size();
}
}
class
Data
{
/**/
///
<summary>
///
用二分法查找数组
///
</summary>
int
number;
//
数组大小;
int
[] list;
//
数组;
int
lower;
int
upper;
int
current;
public
Data(
int
max)
{
list
=
new
int
[max];
number
=
0
;
}
public
void
add(
int
o)
{
int
i;
for
( i
=
0
; i
<
number; i
++
)
if
((
int
)list[i]
>
(
int
)o)
break
;
for
(
int
k
=
number; k
>
i ; k
--
)
{
list[k]
=
list[k
-
1
];
}
list[i]
=
o;
number
++
;
}
public
Boolean cancel(
int
o)
{
int
i
=
find(o);
if
(i.Equals(
"
0
"
))
return
false
;
else
{
for
(
int
k
=
i; k
<
number; k
++
)
{
list[k]
=
list[k
+
1
];
}
number
--
;
return
true
;
}
}
public
int
find(
int
o)
{
lower
=
0
;
upper
=
number
-
1
;
while
(
true
)
{
current
=
(lower
+
upper)
/
2
;
if
(list[current]
==
o)
return
current;
else
if
(lower
>
upper)
return
0
;
else
if
(list[current]
>
(
int
)o)
{
upper
=
current
-
1
;
}
else
if
(list[current]
<
(
int
)o)
{
lower
=
current
+
1
;
}
}
//
return ;
}
public
void
size()
{
Console.WriteLine( number);
}
public
void
display()
{
for
(
int
i
=
0
; i
<
number; i
++
)
{
Console.WriteLine(
"
值:
"
+
list[i]);
}
Console.ReadLine();
}
}
}
posted @
2008-05-25 17:32
啊文
阅读(
1753
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告