Bubble Sort 冒泡排序(C#)

严重缺钱,找了份短期派遣工作.

因为开口说的语言能力还算新手,并且是做原来完全没接触过的C#, 所以老板给找了几个语言好的,经验和我差不多的同事带我.

但似乎工作速度有点过快, 上周五中午就完成了所有工作,甚至基本不加班.
结果,这周四他们还没完成.
上周日,老板专门找我谈话,要我照顾一下大家的情绪,不要把所有精力放在工作上,有空多学学别的.

在尝试学语言,研究源程序代码等等过程,而且不能让大家发现的情况下,

终于,找到了一个似乎在办正事,
我又感兴趣的事情:用C#书写算法代码.
以下是我根据算法的实现方式写得冒泡排序代码,欢迎指正.
BTW,没有中文输入法,只能用Google翻译写拼音来写文章真累.

using System;

namespace ConsoleApp1
{
    class BubbleSort
    {
        static void Main(string[] args)
        {


            int[] list = { 5, 3, 7, 11, 6, 10, 2, 4, 1 };
            //int[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
            //int[] list = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
            bubbleSortMethod( list );
            //selectionSortMethod( list );



            Console.ReadKey();
        }

        static void bubbleSortMethod(int[] sortList )
        {
            int temp = 0;
            int exchangeCnt = 0;
            int endLenth = sortList.Length;
            do
            {
                Console.WriteLine( "endLenth:{0}", endLenth );
                Console.WriteLine( "------------------------------------------------" );
                for ( int i = 0; i < sortList.Length; i++ )
                {
                    Console.WriteLine( sortList[ i ].ToString() );
                }
                Console.WriteLine( "------------------------------------------------" );

                exchangeCnt = 0;
                for ( int i = 0; i < endLenth - 1; i++ )
                {
                    if ( sortList[ i ] > sortList[ i + 1 ] )
                    {
                        temp = sortList[ i + 1 ];
                        sortList[ i + 1 ] = sortList[ i ];
                        sortList[ i ] = temp;
                        exchangeCnt += 1;
                    }
                }
                endLenth -= 1;



            } while ( exchangeCnt > 0 & endLenth > 1 );

            Console.WriteLine( "------------------------------------------------" );
            Console.WriteLine( "The final sortList is :" );

            for ( int i = 0; i < sortList.Length; i++ )
            {
                Console.WriteLine( sortList[ i ].ToString() );
            }

            return;
        }

    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值