Go will support a new sorting algorithm in the next version: pdqsort

82ff6e7a6aa7d5e7400cd80e62cf1972.gif

Finishing | Dream Yidan       

Listing | CSDN (ID: CSDNnews)

Following the support of generics in Go 1.18 , Go will support the pdqsort sorting algorithm in the next version, which has once again caused a heated discussion among developers.

192e7e06c4cdae0aeb0e860de0051279.png

At present, the relevant functional description of pdqsort is introduced in the latest commit of the Go repository:

  • In all benchmarks, pdqsort did not appear to be slower than other previous algorithms;

  • In common patterns, pdqsort is generally faster (i.e. 10x faster in sorting slices)

af1b1ddd311b0cf736fdc64566cdb0a2.png

498eb486a2da8b803d2a1427832d768b.png

What is the pdqsort sorting algorithm?

pdqsort, short for Pattern-defeating quicksort, is a novel sorting algorithm that combines the fast average case of random quicksort with the worst case fast of heap sort, while achieving linear time on inputs with specific patterns. pdqsort is an extension and improvement of David Mussers' introsort. All code is free under the zlib license.

It is currently implemented in C++ and Rust, and according to the actual measurement of many developers, pdqsort, which is more commonly used introsort, will have a greater performance improvement.

  • C++ implementation: https://github.com/orlp/pdqsort

  • Rust implementation: https://docs.rs/pdqsort/latest/pdqsort/

C++ code Demo:

#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");
}

Results of the:

0 1 2 3 4 5 6 7 8 9 : sorted with the default operator< 9 8 7 6 5 4 3 2 1 0 : sorted with the standard library compare function object 0 1 2 3 4 5 6 7 8 9 : sorted with a custom function object 9 8 7 6 5 4 3 2 1 0 : sorted with a lambda expression

Rust code Demo:

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]);

As for Go's support for the pdqsort algorithm, there has been a lot of discussion on HN. Some people said: We have been studying sorting algorithms for so long, and we are surprised that we can still come up with optimization schemes that can produce actual improvements. What do you think about this, let's get started and experience it.

Reference link:

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

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

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

5c0730708ea0f9467d38092bd3582981.png

END

This article is from the upcoming "New Programmer 004", a dialogue with world-class masters, and a report on the innovation and creation of China's IT industry!

03bc3d3a4cd15813ebc26bdd2eb766e9.png

 
  

— Recommended reading —

 
  
☞腾讯被曝要求员工还清90万房贷再离职;苹果因不附带充电器被判赔偿消费者7000元;Git 2.6发布|极客头条
☞《程序员延寿指南》登GitHub热榜,最多可增寿20年?
☞对话PostgreSQL作者Bruce:“转行”是为了更好地前行

—Click here↓↓↓ Remember to pay attention to the stars~- 

"Share", "Like" and "Watching" with one click

Achieve 100 million technicians

62a45e78dc2763e76c6c2668eaa79b8b.png

Guess you like

Origin blog.csdn.net/csdnnews/article/details/124335579