当前位置: 移动技术网 > IT编程>开发语言>c# > c#冒泡排序算法示例

c#冒泡排序算法示例

2019年07月18日  | 移动技术网IT编程  | 我要评论

复制代码 代码如下:

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();

        }
    }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网