加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#冒泡排序算法示例

发布时间:2020-12-15 04:04:14 所属栏目:百科 来源:网络整理
导读:复制代码 代码如下: 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;

复制代码 代码如下:

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

        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读