欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#_实现冒泡排序

程序员文章站 2022-08-24 10:34:57
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 < int ......

 

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