You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#排序测试中冒泡排序比选择排序运行更快的原因是什么?

冒泡排序与选择排序耗时对比异常问题

我正在做一个对比冒泡排序(bubble sort)与选择排序(selection sort)耗时的项目,我原先编写了两个独立程序,将其合并后发现冒泡排序的运行速度远快于选择排序。我已排查确认代码不存在转换错误导致返回0值的问题,运行逻辑符合预期。我使用System.Diagnostics;库进行计时,同时确认问题并非出在运行设备上,在Replit平台运行也得到了相似结果。

相关代码如下:

{
    class Program
    {
       public static int s1 = 0;
       public static int s2 = 0;
        static decimal bubblesort(int[] arr1)
        {
            int n = arr1.Length;
            var sw1 = Stopwatch.StartNew();
            for (int i = 0; i < n - 1; i++)
            {
                for (int j = 0; j < n - i - 1; j++)
                {
                    if (arr1[j] > arr1[j + 1])
                    {
                        int tmp = arr1[j];
                        // swap tmp and arr[i] int tmp = arr[j];
                        arr1[j] = arr1[j + 1];
                        arr1[j + 1] = tmp;
                        s1++;
                    }
                }
            }
            sw1.Stop();
            // Console.WriteLine(sw1.ElapsedMilliseconds);
            decimal a = Convert.ToDecimal(sw1.ElapsedMilliseconds);
            return a;
        }

        static decimal selectionsort(int[] arr2)
        {
            int n = arr2.Length;
            var sw1 = Stopwatch.StartNew();
           // for (int e = 0; e < 1000; e++)
           // {

                for (int x = 0; x < arr2.Length - 1; x++)
                {
                    int minPos = x;
                    for (int y = x + 1; y < arr2.Length; y++)
                    {
                        if (arr2[y] < arr2[minPos])
                            minPos = y;
                    }
                    if (x != minPos && minPos < arr2.Length)
                    {
                        int temp = arr2[minPos];
                        arr2[minPos] = arr2[x];
                        arr2[x] = temp;
                        s2++;
                    }
                }

          //  }
            sw1.Stop();
           // Console.WriteLine(sw1.ElapsedMilliseconds);
            decimal a = Convert.ToDecimal(sw1.ElapsedMilliseconds);
            return a;
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Enter the size of n");
            int n = Convert.ToInt32(Console.ReadLine());
            Random rnd = new System.Random();
            decimal bs = 0M;
            decimal ss = 0M;
            int s = 0;
            int[] arr1 = new int[n];
            int tx = 1000; //tx is a variable that I can use to adjust sample size
            decimal tm = Convert.ToDecimal(tx);

            for (int i = 0; i < tx; i++)
            {

                for (int a = 0; a < n; a++)
                {
                    arr1[a] = rnd.Next(0, 1000000);
                }

                

                ss += selectionsort(arr1);
                bs += bubblesort(arr1);
            }

            bs = bs / tm;
            ss = ss / tm;

            Console.WriteLine("Bubble Sort took " + bs + " miliseconds");
            Console.WriteLine("Selection Sort took " + ss + " miliseconds");


        }
    }
}

请问这是什么情况?是什么原因导致冒泡排序速度偏快,或是选择排序运行被拖慢?我该如何修复该问题?


问题排查结果

我已找到问题原因:选择排序方法中原本每运行一次就会额外执行1000次循环,叠加采样用的1000次外层循环后,导致该方法的性能表现远差于冒泡排序。感谢大家的帮助,也感谢TheGeneral为我介绍了基准测试工具。另外传入方法的数组是值拷贝而非引用传递,手动遍历循环验证可知冒泡排序是正常执行排序逻辑,并非对已排序数组进行操作。

内容的提问来源于stack exchange,提问作者STU1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 00:39:04