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

c#冒泡排序算法示例

程序员文章站 2024-02-22 19:57:46
复制代码 代码如下:using system;using system.collections.generic;using system.linq;using system...

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;

namespace 冒泡排序
{
    class program
    {
        static void swap( ref int  atemp, ref int  btemp)//注意ref的使用
        {
            int temp = atemp;
            atemp = btemp;
            btemp = temp;
        }
        static void main(string[] args)
        {
            int temp=0;
            int[]arr={23,44,66,76,98,11,3,9,7};
            console.writeline("排序前的数组:");
            foreach(int item in arr)
            {
                console.write(item+" ");
            }
            console.writeline();
            for(int i=0;i<arr.length-1;i++)
            {
                for(int j=0;j<arr.length-1-i;j++)
                {
                    if (arr[j] > arr[j + 1])
                          swap( ref  arr[j], ref arr[j + 1]);
                }
            }
           console.writeline("排序后的数组:");
           foreach(int item in arr)
           {
               console.write(item+" ");
            }
            console.writeline();
            console.readkey();

        }
    }
}