網(wǎng)站首頁 編程語言 正文
繼Go 1.18支持泛型后,Go 將在下個版本中支持pdqsort排序算法再次引起了開發(fā)者們的熱切討論。
目前,Go倉庫的最新commit中提交了pdqsort的相關(guān)功能描述:
- 在所有基準(zhǔn)測試中,pdqsort未表現(xiàn)出比以前的其它算法慢;
- 在常見模式中,pdqsort通常更快(即在排序切片中快10倍)
那么pdqsort是什么呢?
pdqsort是Pattern-defeating quicksort的縮寫,是一種新型的排序算法,將隨機(jī)快速排序的快速平均情況與堆排序的最壞情況快速組合在一起,同時(shí)在具有特定模式的輸入上實(shí)現(xiàn)了線性時(shí)間。 pdqsort是David Mussers introsort的擴(kuò)展和改進(jìn)。 在zlib許可下,所有代碼都是免費(fèi)的。
目前在C++和Rust中均有實(shí)現(xiàn),而據(jù)不少開發(fā)者實(shí)測發(fā)現(xiàn),pdqsort較常用的introsort會有較大的性能提升。
- C++ 實(shí)現(xiàn): https://github.com/orlp/pdqsort
- Rust 實(shí)現(xiàn): https://docs.rs/pdqsort/latest/pdqsort/
C++ 代碼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");
}
執(zhí)行結(jié)果:
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 代碼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]);
而就Go支持pdqsort算法,在HN上引起了不少的討論,有人表示,我們研究排序算法這么久了,很驚訝我們還能想出能產(chǎn)生實(shí)際改進(jìn)的優(yōu)化方案。對此,你怎么看,快快上手體驗(yàn)一下吧。
參考鏈接:
https://github.com/golang/go/commit/72e77a7f41bbf45d466119444307fd3ae996e257
https://news.ycombinator.com/item?id=31106157
https://en.cppreference.com/w/cpp/algorithm/sort
原文鏈接:https://blog.csdn.net/csdnnews/article/details/124323267
相關(guān)推薦
- 2022-11-13 C#實(shí)現(xiàn)定義一個通用返回值_C#教程
- 2022-03-13 使用vs2022在.net6中調(diào)試帶typescript的靜態(tài)頁面_基礎(chǔ)應(yīng)用
- 2022-06-18 datagridview實(shí)現(xiàn)手動添加行數(shù)據(jù)_C#教程
- 2022-05-28 C語言?超詳細(xì)講解庫函數(shù)_C 語言
- 2022-02-14 flutter封裝自定義打印信息
- 2022-08-05 EasyExcel 3.X 簡單寫入Excel文件數(shù)據(jù)
- 2022-05-29 python3中的類繼承你真的了解嗎_python
- 2022-01-22 redis——緩存穿透、緩存擊穿、緩存雪崩、分布式鎖
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支