[排序] 冒泡排序
using System;
using System.Collections.Generic;
using System.Text;
namespace AlgoLibrary.Sort
{
/// <summary>
/// Summary description for BubbleSort.
/// </summary>
public class BubbleSort
{
private int[] _ary;
private int _count;
public int Count
{
get { return _count; }
}
/// <summary>
/// 默认构造函数
/// </summary>
/// <param name="ary"></param>
public BubbleSort(int[] ary)
{
this._ary = ary;
this._count = _ary.Length;
}
/// <summary>
/// 排序
/// </summary>
public void Sort()
{
int temp = 0;
if (_ary.Length <= 1)
{
return;
}
else if (_ary.Length == 2)
{
if (_ary[0] > _ary[1])
{
temp = _ary[0];
_ary[0] = _ary[1];
_ary[1] = temp;
return;
}
}
for (int i = 0; i < _ary.Length-1; i++)
{
for (int j = i+1; j < _ary.Length; j++)
{
if (_ary[i] > _ary[j])
{
temp = _ary[i];
_ary[i] = _ary[j];
_ary[j] = temp;
}
}
}
}
/// <summary>
/// 重写基类 ToString() 方法
/// </summary>
/// <returns></returns>
public override string ToString()
{
string str = string.Empty;
for (int i = 0; i < _ary.Length; i++)
{
str += _ary[i].ToString() + " ";
}
return str;
}
}//class BubbleSort
}//namespace AlgoLibrary.Sort


浙公网安备 33010602011771号