Fastest way to sort an array in descending order

前端 未结 4 1151
春和景丽
春和景丽 2020-12-15 20:51

Why is the following code

Array.Sort(values);
Array.Reverse(values);

much faster at sorting an array in descending order compared to

<
4条回答
  •  有刺的猬
    2020-12-15 21:05

    As the link points out

    Sort method here always ends up in an internal TrySZSort or QuickSort method when it doesn't throw an exception. The TrySZSort internal method is optimized for one-dimensional arrays, also known as "Zero" arrays or vectors

    Because the TrySZSort method used in the base class libraries is implemented in native code, it has been heavily optimized. Therefore, this method is likely faster than any solution written in the C# language

提交回复
热议问题