冒泡排序的几种实现方式

 

代码
/// <summary>
/// 把最大的依次换到最下方
/// </summary>
protected void BobbleSortUpToDown()
{

int[] s = { 10,20,30,40,50,60 };
for (int i = 0; i < s.Length - 1; i++)
{

bool exchange = false;
Response.Write(i.ToString()
+ ":");
for (int j = 0; j < s.Length - i - 1; j++)
{
if (s[j] > s[j + 1])
{
int temp = s[j];
s[j]
= s[j + 1];
s[j
+ 1] = temp;

exchange
= true;
}
Response.Write(j.ToString());
}
Response.Write(
"<br/>");
if (!exchange)
return;
}
for (int i = 0; i < s.Length; i++)
{
Response.Write(s[i].ToString());
}


}


/// <summary>
/// 把最小的依次换到最上方
/// j向上走
/// </summary>
protected void BobbleSortDownToUp()
{

int[] s = { 50,60 , 40, 30, 20, 10 };
for (int i = 0; i < s.Length - 1; i++)
{
Response.Write(
"<br/>"+i.ToString() + ":");
for (int j = s.Length - 2; j >=i; j--)
{
if (s[j] > s[j + 1])
{
int temp = s[j];
s[j]
= s[j + 1];
s[j
+ 1] = temp;
}
Response.Write(j.ToString());
}
Response.Write(
"<br/>");
}
for (int i = 0; i < s.Length; i++)
{
Response.Write(s[i].ToString());
}


}

/// <summary>
/// 把最小的放在上面
/// j向下走
/// </summary>
protected void BobbleSortDownToUp3()
{


int[] list = { 19, 11, 15, 12, 55, 90, 60 };
for (int i = 0; i < list.Length; i++)
{
for (int j = i + 1; j < list.Length; j++)
{
if (list[i] > list[j])
{
int temp = list[i];
list[i]
= list[j];
list[j]
= temp;
}
}
}
for (int i = 0; i < list.Length; i++)
{
Response.Write(list[i]);
}
}

 

posted on 2010-05-19 18:50  bobo327  阅读(486)  评论(0编辑  收藏  举报