一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net c#冒泡排序算法实现代码

时间:2022-06-25 08:24:27 编辑:袖梨 来源:一聚教程网

1、用第三个变量值交换

int a = 1;
int b = 2;
int c = a;
a = b;
b = c;
2、用加减法交换

a = a + b;
b = a - b;
a = a - b;

a = a - b;
b = a + b;
a = b - a;3、用异或(XOR)法交换

 a = a ^ b;
 b = a ^ b;
 a = a ^ b;详细可看codeproject上一篇文章 <<神奇的异或 The Magical Exclusive OR (XOR)>>下面我们要在冒泡排序算法中测试一下这三种交换方法的效率

 代码如下 复制代码

  public enum EChangeType
    {
        ///


        /// 用第三方变量temp进行前后值交换
        ///

        ThridVar,
        ///
        /// 用a=a+b;b=a-b;a=a-b 计算方法进行前后值交换
        ///

        Js,
        ///
        /// 用a=a^b;b=a^b;a=a^b 计算方法进行前后值交换
        ///

        XOR
    }///
    /// 冒泡排序算法:
    /// a、首先将所有待排序的数字放入工作列表中
    /// b、从列表的第一个数字到倒数第二个数字,逐个检查:若某一位上的数字大于他的下一位,则将它与它的下一位交换
    /// c、重复2号步骤,直至再也不能交换
    ///

    public class BubbleSort
    {
        public void Sort(int[] arrInt, EChangeType type)
        {
            System.Diagnostics.Stopwatch st = new System.Diagnostics.Stopwatch();
            st.Start();
            int count = 0;
            int temp;
            bool hasExchangeAction; //记录此次大循环中相邻的两个数是否发生过互换(如果没有互换,则数组已经是有序的)

            for (int i = 0; i < arrInt.Length - 1; i++)
            {
                count++;
                hasExchangeAction = false;  //每次大循环都假设数组有序

                for (int j = i + 1; j < arrInt.Length; j++)
                {
                    count++;
                    if (arrInt[i] > arrInt[j])
                    {
                        switch (type)
                        {
                            case EChangeType.ThridVar:
                                temp = arrInt[i];
                                arrInt[i] = arrInt[i + 1];
                                arrInt[i + 1] = temp;
                                break;
                            case EChangeType.Js:
                                arrInt[i] = arrInt[i] + arrInt[j];
                                arrInt[j] = arrInt[i] - arrInt[j];
                                arrInt[i] = arrInt[i] - arrInt[j];
                                break;
                            case EChangeType.XOR:
                                arrInt[i] = arrInt[i] ^ arrInt[j];
                                arrInt[j] = arrInt[i] ^ arrInt[j];
                                arrInt[i] = arrInt[i] ^ arrInt[j];
                                break;
                            default:
                                break;
                        }
                        hasExchangeAction = true;   //发生过互换
                    }
                }

                if (!hasExchangeAction) //如果没有发生过互换,则数组已经是有序的了,跳出循环
                {
                    break;
                }
            }
            st.Stop();
            switch (type)
            {
                case EChangeType.ThridVar:
                    Console.WriteLine("用第三方变量temp进行前后值交换:");
                    break;
                case EChangeType.Js:
                    Console.WriteLine("用a = a + b; b = a - b; a = a - b;(或a = a - b; b = a + b; a = b - a;) n计算方法进行前后值交换:");
                    break;
                case EChangeType.XOR:
                    Console.WriteLine("用a = a ^ b; b = a ^ b; a = a ^ b; 计算方法进行前后值交换:");
                    break;
                default:
                    break;
            }
            Console.WriteLine("循环总数:{0} 排序耗时:{1}{2}", count, st.ElapsedMilliseconds,Environment.NewLine);
        }
    }

static void Main(string[] args)
        {
            List list = new List();
            for (int i = 100 * 100; i >= 0; i--)
            {
                list.Add(i);
            }
            Console.WriteLine("对100 * 100个数字进行冒泡排序{0}", Environment.NewLine);
       
            new BubbleSort().Sort(list.ToArray(), EChangeType.ThridVar);
        
            new BubbleSort().Sort(list.ToArray(), EChangeType.Js);         
      
            new BubbleSort().Sort(list.ToArray(), EChangeType.XOR);
       
            Console.ReadLine();
        }结果

 

人生无处不PK

热门栏目