当前位置:网站首页>Parallelized Quick Sort Ideas
Parallelized Quick Sort Ideas
2022-07-30 12:53:00 【Jun Meng Ru Yan Brian】
Parallelized Quicksort
For general quicksort, always divide the task into two subtasks using divide and conquer.Then, the subtasks are opposed to each other.Not only that, it is not like the merge sort process that needs to be aggregated after the divide and conquer.
Therefore, it's natural to associate a quicksort trip with a mission.
Specification: QuickSort(arr, l, r) is a quick sort.
Since the tasks are independent of each other, after each quicksort is completed (that is, after confirming the subscript of a number), we can execute the subtasks in parallel and turn them into two subtasks in the range of segments.Less tasks, we can use the insertion algorithm to handle directly.
Because it is a computationally intensive program, in order to maximize performance, we directly design a thread pool to reuse threads.
void QuickSort(arr, beg, end){if(end - beg <= 5) {inserted_sort(arr, beg, end);return;}mid = beg + (end - beg)/2;pool.addTask(QuickSort(arr, beg, mid));pool.addTask(QuickSort(arr, mid, end));}Actually, it's no different from the recursive version of Quicksort
About synchronization, as long as a customized version of CountDownLatch is used, the main thread waits, and the count increases every time a task is delivered, and then goes down at the end of the function to prevent premature down from causing the main thread to wake up early.
void QuickSort(arr, beg, end){if(end - beg <= 5) {inserted_sort(arr, beg, end);return;}mid = beg + (end - beg)/2;pool.addTask(QuickSort(arr, beg, mid));pool.addTask(QuickSort(arr, mid, end));latch.inc(2); // count = count + 2;latch.down(); // count = count - 1;}Part of the main thread
threadpool pool;CountDownLatch latch;latch.inc(1);pool.addTask(QuickSort(arr, beg, end))latch.wait();About Parallelizing Merge Sort
The parallelization of merge sort is similar to quicksort.It just needs to be synchronized when the two subtasks are ended.In terms of performance, it should not be as fast as quicksort.Because of synchronization between task threads, we can't reuse threads well.Of course, we can solve this blocking problem with methods similar to coroutines.
边栏推荐
猜你喜欢

概率论得学习和整理6:概率的分布

如何把Excel表格显示到邮件正文里?

监控界的最强王者,没有之一!

MySQL查询性能优化

PyQt5快速开发与实战 8.2 绘图 && 8.3 QSS的UI美化

Using Baidu EasyDL to realize the recognition of the chef's hat of the bright kitchen

概率论的学习和整理7:理解期望和方差还是要回到随机试验本身,期望不是平均值,方差的公式不同情况不同

AlphaFold预测了几乎所有已知蛋白质!涵盖100万物种2.14亿结构,数据集开放免费用...

奇异值分解(SVD)原理与在降维中的应用(附带例题讲解)(纯理论)

Homework 7.29 correlation function directory and file attributes related functions
随机推荐
刷屏了!!!
最基础01/完全背包
int a=8,a=a++,a? int b=8,b=b+1,b?
int a=8,a=a++,a? int b=8,b=b+1,b?
shell的理解
[BJDCTF2020]Cookie is so stable-1|SSTI injection
Beijing, Shanghai and Guangzhou offline events丨The most unmissable technology gatherings at the end of the year are all gathered
电流电压采集模块DAM-6160
基于卷积神经网络与双向长短时融合的锂离子电池剩余使用寿命预测
[PostgreSQL] - 存储结构及缓存shared_buffers
Heshu Group: Make smart cities smarter and make real life better
常见的云计算安全问题以及如何解决
RTSP/Onvif协议视频平台EasyNVR服务一键升级功能的使用教程
Zhou Hongyi: Microsoft copied the 360 security model and became the largest security company in the United States
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
js 构造函数 return 非空对象,其实例化的对象在原型上的差异
维护数千规模MySQL实例,数据库灾备体系构建指南
Xms Xmx PermSize MaxPermSize 区别
How to solve the problem that the page does not display the channel configuration after the EasyNVR is updated to (V5.3.0)?
数字化时代,寻求企业财务转型路径的最优解