Goは、次のバージョンでpdqsortソートアルゴリズムをサポートします

Go 1.18でのジェネリックスのサポートに続いて、Goは次のバージョンでpdqsortソートアルゴリズムをサポートします。これにより、開発者の間で再び白熱した議論が発生しました。

ここに画像の説明を挿入

現在、 pdqsortの関連する関数の説明は、 Goリポジトリの最新のコミットで送信されます。

  • すべてのベンチマークで、pdqsortは他の以前のアルゴリズムよりも遅いようには見えませんでした。
  • 一般的なパターンでは、pdqsortは一般的に高速です(つまり、スライスの並べ替えで10倍高速です)

では、pdqsortとは何ですか?

パターンを無効にするクイックソートの略であるpdqsortは、ランダムクイックソートの高速平均ケースとヒープソートの最悪ケースの高速を組み合わせ、特定のパターンの入力で線形時間を達成する新しいソートアルゴリズムです。pdqsortは、DavidMussersイントロソートの拡張および改良です。すべてのコードはzlibライセンスの下で無料です。

現在、C ++とRustの両方で実装されています。多くの開発者の実際の測定によると、pdqsortのより一般的に使用されるイントロソートではパフォーマンスが大幅に向上します。

C ++コードのデモ:

#include <algorithm>
#include <functional>
#include <array>
#include <iostream>
#include <string_view>
 
int main()
{
    
    
    std::array<int, 10> s = {
    
    5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
 
    auto print = [&s](std::string_view const rem) {
    
    
        for (auto a : s) {
    
    
            std::cout << a << ' ';
        }
        std::cout << ": " << rem << '\n';
    };
 
    std::sort(s.begin(), s.end());
    print("sorted with the default operator<");
 
    std::sort(s.begin(), s.end(), std::greater<int>());
    print("sorted with the standard library compare function object");
 
    struct {
    
    
        bool operator()(int a, int b) const {
    
     return a < b; }
    } customLess;
    std::sort(s.begin(), s.end(), customLess);
    print("sorted with a custom function object");
 
    std::sort(s.begin(), s.end(), [](int a, int b) {
    
    
        return a > b;
    });
    print("sorted with a lambda expression");
}

結果:

0 1 2 3 4 5 6 7 8 9:デフォルトの演算子で並べ替え<
9 8 7 6 5 4 3 2 1 0:標準ライブラリの比較関数オブジェクトで並べ替え
0 1 2 3 4 5 6 7 8 9:で並べ替えカスタム関数オブジェクト987
6 5 4 3 2 1 0:ラムダ式で並べ替え

さびコードデモ:

extern crate pdqsort;

let mut v = [-5i32, 4, 1, -3, 2];

pdqsort::sort(&mut v);
assert!(v == [-5, -3, 1, 2, 4]);

pdqsort::sort_by(&mut v, |a, b| b.cmp(a));
assert!(v == [4, 2, 1, -3, -5]);

pdqsort::sort_by_key(&mut v, |k| k.abs());
assert!(v == [1, 2, -3, 4, -5]);

Goによるpdqsortアルゴリズムのサポートについては、HNについて多くの議論がありましたが、ソートアルゴリズムを長い間研究しているとの意見もありましたが、実際に生成できる最適化スキームを思いつくことができることに驚いています。改善。これについてどう思いますか、始めて体験しましょう。

参照リンク:

  • https://github.com/golang/go/commit/72e77a7f41bbf45d466119444307fd3ae996e257

  • https://news.ycombinator.com/item?id=31106157

  • https://en.cppreference.com/w/cpp/algorithm/sort

おすすめ

転載: blog.csdn.net/csdnnews/article/details/124323267