C#_实现冒泡排序

时间:2023-03-09 07:33:47
C#_实现冒泡排序
//排序方法类
public class Bubble
{
public static int SizeCount=;
public static void SBubble(ref int[] intArr)
{
for (int outSize = ; outSize < intArr.Length-; outSize++)
{
for (int index = ; index < intArr.Length--outSize; index++)
{
SizeCount ++;
if (intArr[outSize]>intArr[index+])
{
intArr[index] = intArr[index] + intArr[index + ];
intArr[index + ] = intArr[index]-intArr[index + ];
intArr[index] = intArr[index] - intArr[index + ];
} } } }
}
//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();
}