【问题标题】:what's the fastest kind of sorting an array?排序数组最快的类型是什么?
【发布时间】:2012-03-03 13:58:39
【问题描述】:

我使用选择排序,但我想要一个比这更好的排序解决方案:

    static void SelectionSort(int[] a)
    {
        int temp, pmin;
        for (int i=0; i<a.Length-1; i++)
        {
            pmin=i;
            for (int j=i+1; j<a.Length; j++)
            if (a[j]<a[pmin])
                pmin=j;
            temp = a[i];
            a[i] = a[pmin];
            a[pmin] = temp;
        }
    }

【问题讨论】:

标签: c# sorting


【解决方案1】:

只需使用:

Array.Sort(a);

这将执行快速排序。

【讨论】:

    【解决方案2】:

    如果您查看wikipedia,就空间和时间复杂度而言,可以很好地比较不同的排序算法。许多是n lg nn 平均时间,应该很好地满足您的需求。

    此外,.NET 内置了多种排序算法。包括Array.Sort()List.Sort()

    【讨论】:

      【解决方案3】:

      linq 中使用Array.Sort(a)orderby

      【讨论】:

        【解决方案4】:

        排序算法没有绝对的赢家。效率取决于数组的大小、内容、状态。 只知道什么是最适合你的。

        查看here 获取示例和测量结果。

        【讨论】:

          【解决方案5】:

          只需使用Array中的内置函数:Array.Sort(a)

          【讨论】:

            【解决方案6】:

            要获得最快的排序算法,您应该使用heap sort。与所有其他排序算法相比,它的时间复杂度最低

            A program to implement Heap Sort
            
            #include<stdio.h>
            
            void restoreHup(int*,int);
            void restoreHdown(int*,int,int);
            
            void main()
            {
                int a[20],n,i,j,k;
                printf("   Enter the number of elements to sort : ");
                scanf("%d",&n);
            
                printf("Enter the elements : ");
                for(i=1;i<=n;i++){
                        scanf("%d",&a[i]);
                        restoreHup(a,i);
                }
            
            
                j=n;
                for(i=1;i<=j;i++)
                {
                        int temp;
                        temp=a[1];
                        a[1]=a[n];
                        a[n]=temp;
                        n--;
                        restoreHdown(a,1,n);
                }
            
                n=j;
            
                printf("Here is it...");
                for(i=1;i<=n;i++)
                        printf("%4d",a[i]);
                }
            
            
            
                void restoreHup(int *a,int i)
                {
                 int v=a[i];
            
                 while((i>1)&&(a[i/2]<v))
                 {
                        a[i]=a[i/2];
                        i=i/2;
                 }
                 a[i]=v;
                }
            
                void restoreHdown(int *a,int i,int n)
               {
                int v=a[i];
                int j=i*2;
                while(j<=n)
                {
                        if((j<n)&&(a[j]<a[j+1]))
                                j++;
                        if(a[j]<a[j/2]) break;
            
                        a[j/2]=a[j];
                        j=j*2;
                }
                a[j/2]=v;
               }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-09-22
              • 1970-01-01
              • 2010-12-04
              • 2021-10-26
              相关资源
              最近更新 更多