会员
众包
新闻
博问
闪存
赞助商
HarmonyOS
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
冬眠的蛤蟆
春醒,夏泳,秋思,冬眠…… MSN: "netflu"+"@"+"msn.com"
博客园
首页
新随笔
联系
订阅
管理
[排序] 双向冒泡排序
using
System;
using
System.Collections.Generic;
using
System.Text;
namespace
AlgoLibrary.Sort
{
/**/
///
<summary>
///
Summary description for DBubbleSort.
///
</summary>
public
class
DBubbleSort
{
private
int
[] _ary;
private
int
_count;
public
int
Count
{
get
{
return
_count; }
}
/**/
///
<summary>
///
默认构造函数
///
</summary>
///
<param name="ary"></param>
public
DBubbleSort(
int
[] ary)
{
this
._ary
=
ary;
this
._count
=
_ary.Length;
}
/**/
///
<summary>
///
排序
///
</summary>
public
void
Sort()
{
if
(Count
<=
1
)
{
return
;
}
else
if
(Count
==
2
)
{
if
(_ary[
0
]
>
_ary[
1
])
{
int
temp
=
_ary[
0
];
_ary[
0
]
=
_ary[
1
];
_ary[
1
]
=
temp;
return
;
}
}
int
low
=
0
;
int
up
=
Count
-
1
;
while
(low
<
up)
{
for
(
int
i
=
low; i
<
up; i
++
)
{
if
(_ary[up]
<
_ary[i])
{
int
temp
=
_ary[up];
_ary[up]
=
_ary[i];
_ary[i]
=
temp;
}
}
for
(
int
i
=
low
+
1
; i
<
up; i
++
)
{
if
(_ary[low]
>
_ary[i])
{
int
temp
=
_ary[low];
_ary[low]
=
_ary[i];
_ary[i]
=
temp;
}
}
low
++
;
up
--
;
}
}
/**/
///
<summary>
///
重写基类 ToString() 方法
///
</summary>
///
<returns></returns>
public
override
string
ToString()
{
string
str
=
string
.Empty;
for
(
int
i
=
0
; i
<
Count; i
++
)
{
str
+=
_ary[i].ToString()
+
"
"
;
}
return
str;
}
}
//
class DBubbleSort
}
//
namespace AlgoLibrary.Sort
posted @
2007-06-15 13:37
蛤蟆
阅读(
801
) 评论(
0
)
收藏
举报
刷新页面
返回顶部
公告