[排序] 双向冒泡排序
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


浙公网安备 33010602011771号