C#_实现冒泡排序

//排序方法类
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();
}






原文地址:https://www.cnblogs.com/100234ltf/p/9900569.html