VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#教程之C#_实现冒泡排序

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

 

复制代码
//排序方法类

public class Bubble
{
public static int SizeCount=0;
public static void SBubble(ref int[] intArr)
{
for (int outSize = 0; outSize < intArr.Length-1; outSize++)
{
for (int index = 0; index < intArr.Length-1-outSize; index++)
{
SizeCount ++;
if (intArr[outSize]>intArr[index+1])
{
intArr[index] = intArr[index] + intArr[index + 1];
intArr[index + 1] = intArr[index]-intArr[index + 1];
intArr[index] = intArr[index] - intArr[index + 1];
}

}


}


}
}
//Main方法

static void Main(string[] args)
{
int[] intArr = new int[10] { 51, 41, 31, 91, 81, 71, 61, 21, 11, 0 };


Console.Write("排序前:");
for (int i = 0; i < intArr.Length; i++)
{
Console.Write(intArr[i] + " ");
}
Console.WriteLine();


//Bubble_Sort(ref intArr);
Bubble.SBubble(ref intArr);


Console.Write("排序后:");
for (int i = 0; i < intArr.Length; i++)
{
Console.Write(intArr[i] + " ");
}
Console.WriteLine();


Console.WriteLine("计算次数:" + Bubble.SizeCount);


Console.ReadLine();
Console.Read();
}





复制代码

相关教程