简述

计数排序,就是统计某个数值在所有的数字中所应该存在的位置,然后,放到一个确定的位置上。非常简单的排序算法。

程序

  • 会读取data.txt中的文件
  • 数据的样子
10
0 2 3 1 4 8 6 7 5 9
  • 效果
PS D:\C++\VS\repo\OpenMP-TEST\Debug> ./OpenMP-TEST
0 1 2 3 4 5 6 7 8 9
  • 代码
#include <iostream>
#include <omp.h>
#include <fstream>
using namespace std;
#pragma warning(disable : 4996)
int main(int argc, char **argv) {if (argc < 1) return 0;ifstream cin("./data.txt");int i, j, count, n;cin >> n;int *a = new int[n];int *temp = new int[n];//int thread_count = strtol(argv[1], NULL, 10);for (i = 0; i < n; ++i)  cin >> a[i];#pragma omp parallel for private(i, j, count) shared(n, a, temp)for (i = 0; i < n; ++i) {count = 0;for (j = 0; j < n; ++j) {if (a[j] < a[i]) count++;else if (a[j] == a[i] && j < i) count++;}temp[count] = a[i];}#pragma omp parallel for for (i = 0; i < n; ++i) a[i] = temp[i];for (i = 0; i < n; ++i) cout << a[i]<< " ";cout << endl;delete[]temp;delete[]a;
}

和串行的计数排序进行对比

  • 不要输出排序结果,因为数据量太大了。
  • 10000个数据的排序效果
PS D:\C++\VS\repo\OpenMP-TEST\Debug> ./OpenMP-TEST r
Total time Serial: 1.256s
Total time parallel: 0.141s
  • 接近9倍的速度(因为我在笔记本上有8个核)
  • 后面多了一个r命令,其实是免得在vs编译的时候被调用。随便写什么都是ok的。
#include <iostream>
#include <omp.h>
#include <fstream>
#include <ctime>
using namespace std;
#pragma warning(disable : 4996)
int main(int argc, char **argv) {if (argc < 2) return 0;ifstream cin("./data.txt");int i, j, count, n;cin >> n;int *a = new int[n];int *b = new int[n];int *temp = new int[n];//int thread_count = strtol(argv[1], NULL, 10);for (i = 0; i < n; ++i) cin >> b[i];for (i = 0; i < n; ++i) a[i] = b[i];clock_t starttime, endtime;starttime = clock();for (i = 0; i < n; ++i) {count = 0;for (j = 0; j < n; ++j) {if (b[j] < b[i]) count++;else if (b[j] == b[i] && j < i) count++;}temp[count] = b[i];}memcpy(b, temp, n * sizeof(int));endtime = clock();// for (i = 0; i < n; ++i) cout << b[i] << " ";// cout << endl;cout << "Total time Serial: " << (double)(endtime - starttime) / CLOCKS_PER_SEC << "s" << endl;starttime = clock();
#pragma omp parallel for private(i, j, count) shared(n, a, temp)for (i = 0; i < n; ++i) {count = 0;for (j = 0; j < n; ++j) {if (a[j] < a[i]) count++;else if (a[j] == a[i] && j < i) count++;}temp[count] = a[i];}#pragma omp parallel for for (i = 0; i < n; ++i) a[i] = temp[i];endtime = clock();//for (i = 0; i < n; ++i) cout << a[i]<< " ";//cout << endl;cout << "Total time parallel: " << (double)(endtime - starttime) / CLOCKS_PER_SEC << "s" << endl;delete[]temp;delete[]a;delete[]b;
}

和qsort的串行版也做对比

#include <iostream>
#include <omp.h>
#include <fstream>
#include <ctime>
using namespace std;
#pragma warning(disable : 4996)
int cmp(const void * a, const void *b) //from small to big
{return *(int *)a - *(int *)b;
}int main(int argc, char **argv) {if (argc < 2) return 0;ifstream cin("./data.txt");int i, j, count, n;cin >> n;int *a = new int[n];int *b = new int[n];int *c = new int[n];int *temp = new int[n];//int thread_count = strtol(argv[1], NULL, 10);for (i = 0; i < n; ++i) cin >> b[i];for (i = 0; i < n; ++i) { a[i] = b[i]; c[i] = b[i]; }clock_t starttime, endtime;starttime = clock();qsort(c, n, sizeof(int), cmp);endtime = clock();cout << "Total time Serial-qsort: " << (double)(endtime - starttime) / CLOCKS_PER_SEC << "s" << endl;starttime = clock();for (i = 0; i < n; ++i) {count = 0;for (j = 0; j < n; ++j) {if (b[j] < b[i]) count++;else if (b[j] == b[i] && j < i) count++;}temp[count] = b[i];}memcpy(b, temp, n * sizeof(int));endtime = clock();cout << "Total time Serial: " << (double)(endtime - starttime) / CLOCKS_PER_SEC << "s" << endl;starttime = clock();
#pragma omp parallel for private(i, j, count) shared(n, a, temp)for (i = 0; i < n; ++i) {count = 0;for (j = 0; j < n; ++j) {if (a[j] < a[i]) count++;else if (a[j] == a[i] && j < i) count++;}temp[count] = a[i];}#pragma omp parallel for for (i = 0; i < n; ++i) a[i] = temp[i];endtime = clock();//for (i = 0; i < n; ++i) cout << a[i]<< " ";//cout << endl;cout << "Total time parallel: " << (double)(endtime - starttime) / CLOCKS_PER_SEC << "s" << endl;delete[]temp;delete[]a;
}

count_sort计数排序OpenMP的并行化相关推荐

  1. 计数排序之python

    计数排序( Count sort) 一个不需要比较的,类似于桶排序的线性时间排序算法.该算法是对已知数量范围的数组进行排序.其时间复杂度为O(n),适用于小范围集合或重复元素多的排序.计数排序是用来排 ...

  2. 计数排序与桶排序python实现

    计数排序与桶排序python实现 计数排序 计数排序原理: 找到给定序列的最小值与最大值 创建一个长度为最大值-最小值+1的数组,初始化都为0 然后遍历原序列,并为数组中索引为当前值-最小值的值+1 ...

  3. counting sort (计数排序) algorithm

    为什么80%的码农都做不了架构师?>>>    假设n个输入元素中每一个均介于0~k之间,k为最大值,这里k为整数.如果k=O(n), 则计数排序的运行时间为θ(n). 计数排序的核 ...

  4. 1.12_count_sort_计数排序

    --- 计数排序 ---对列表进行排序,已知列表中的数范围都在 0 到 100 之间 设计时间复杂度为 O(n) 的算法限制:1. 知道范围2. 占用空间可能很大 def count_sort(li, ...

  5. 算法导论第八章__实现计数排序

    计数排序:不须要比較就能得出排序的顺序__比如.本章的计数排序.基数排序.桶排序 比較排序:须要进行比較才干得出排序的顺序__比如,本章的堆排序.高速排序(本质是插入排序).插入排序 代码清单:计数排 ...

  6. 计数排序(count sort)

    有这样一道排序题:数组里有20个随机数,取值范围为从0到10,要求用最快的速度把这20个整数从小到大进行排序. 第一时间你可能会想使用快速排序,因为快排的时间复杂度只有O(nlogn).但是这种方法还 ...

  7. 理解计数排序算法的原理和实现

    计数排序(Counting sort)是一种稳定的线性时间排序算法,其平均时间复杂度和空间复杂度为O(n+k),其中n为数组元素的个数,k为待排序数组里面的最大值.同样具有线性时间排序的算法还有桶排序 ...

  8. 计数排序和桶排序——python和javascript实现

    计数排序 python版 不稳定计数排序 # 不稳定计数排序 def count_sort(arr):max,min = findMaxAndMin(arr)space = max - mincoun ...

  9. 计数排序 详解 C代码

    1.计数排序作为一个排序算法有以下几个特点: (1)不基于比较 (2)稳定排序 (3)时间复杂度O(n) (4)只能用在数据范围不大的场景(这个问题在举例以后会在讨论) 2.算法思路  (1) 假设有 ...

最新文章

  1. 神器与经典--sp_helpIndex
  2. 如何查看oracle trace 文件
  3. linux下模拟cc攻击命令,介绍几种用Linux命令判断CC攻击的方法
  4. javaScript原型及继承
  5. 【转载】socket select模型
  6. 挑选回文串(二进制枚举)
  7. Hibernate关系映射和HQL
  8. 概率语言模型及其变形系列-PLSA及EM算法
  9. 面试:C++实现访问者模式
  10. LaTeX在数学环境中使用直立体
  11. 读书:在别人的盯梢儿和嚼舌根中茁壮成长 | 杂谈
  12. linux下安装虚拟天文馆,【地理软件】虚拟天文馆——stellarium
  13. 干货|建模3D Max中常见问题
  14. 以flv.js框架为基础,替换flv格式视频
  15. 基于python的数据爬取与分析_基于Python的网站数据爬取与分析的技术实现策略
  16. Unity Dotween常用方法详解
  17. Android中从视频中提取音频
  18. 全局钩子+正则表达式=后台自动获取扫描枪数据
  19. 表必备三字段:id, gmt_create, gmt_modified
  20. 【交往智慧】004.己所不欲,勿施于人

热门文章

  1. Google首席软件工程师Joshua Bloch谈如何设计一款优秀的API【附PPT】
  2. 山西出台法规规范社会力量认养文物 系全国首例
  3. PHP Warning: Xdebug MUST be loaded as a Zend extension
  4. ng-repeat 的重复问题
  5. 接口是否可继承接口? 抽像类是否可实现(implements)接口? 抽像类是否可继承实体类(concrete class)?...
  6. Markdown学习测试.md
  7. Android开发之搜Ya项目说明(3)
  8. 未能为数据库 '*'中得对象'*'分配空间,因文件组'PRIMARY'已满
  9. 非常美妙的图片,呵呵
  10. php项目打开快捷方式,克隆一个新项目的快捷方式_PHP教程