▶ 学习了双调排序,参考(https://blog.csdn.net/xbinworld/article/details/76408595)

● 使用 CPU 排序的代码

 #include <stdio.h>

 #define LENGTH 1024
#define ASCENDING 1
#define DESCENDING 0 int a[LENGTH]; void compare(int i, int j, int dir)
{
if (dir == (a[i]>a[j]))
{
int h = a[i];
a[i] = a[j];
a[j] = h;
}
} void bitonicMerge01(int lo, int cnt, int dir)// 先再大跨度(半区间长)上调整元素,再递归地在小跨度上进行相同的调整
{
if (cnt > )
{
int k = cnt / ;
for (int i = lo; i < lo + k; i++)
compare(i, i + k, dir);
bitonicMerge01(lo, k, dir);
bitonicMerge01(lo + k, k, dir);
}
} void bitonicSort01(int lo, int cnt, int dir)// 先递归地要求小跨度区间依次排成 “升↗降↘升↗降↘” 再在较大跨度上进行合并
{
if (cnt > )
{
int k = cnt / ;
bitonicSort01(lo, k, ASCENDING);
bitonicSort01(lo + k, k, DESCENDING);
bitonicMerge01(lo, cnt, dir);
}
} void bitonicMerge02(int l, int r, const int dir)
{
if (r - l > )
{
int stride = (r - l) / + ;
for (int i = l; i < l + stride; i++)
compare(i, i + stride, dir);
bitonicMerge02(l, l + stride - , dir);
bitonicMerge02(l + stride, r, dir);
}
} void bitonicSort02(int l, int r, const int dir)
{
if (r - l > )
{
int rNew = l + (r - l) / ;
bitonicSort02(l, rNew, ASCENDING);
bitonicSort02(rNew + , r, DESCENDING);
bitonicMerge02(l, r, dir);
}
} void bitonicMerge03(int l, int r, const int dir)
{
if (r - l > )
{
int stride = (r - l) / ;
for (int i = l; i < l + stride; i++)
compare(i, i + stride, dir);
bitonicMerge03(l, l + stride, dir);
bitonicMerge03(l + stride, r, dir);
}
} void bitonicSort03(int l, int r, const int dir)
{
if (r - l > )
{
int rNew = l + (r - l) / ;
bitonicSort03(l, rNew, ASCENDING);
bitonicSort03(rNew, r, DESCENDING);
bitonicMerge03(l, r, dir);
}
} int main()
{
int i, error;
srand();
for (i = ; i < LENGTH; a[i++] = rand()); printf("\n");
for (i = ; i < LENGTH; i++)
{
printf("%5d,", a[i]);
if ((i + ) % == )
printf("\n");
} //bitonicSort01(0, LENGTH, ASCENDING); // 使用起点和长度
//bitonicSort02(0, LENGTH - 1, ASCENDING); // 使用左端点和右端点(都包含)
bitonicSort03(, LENGTH, ASCENDING); // 使用左端点和右端点(左包含右不包含) printf("\n");
for (i = , error = -; i < LENGTH; i++)
{
printf("%5d,", a[i]);
if (i < LENGTH - && a[i] > a[i + ])
error = i;
if ((i + ) % == )
printf("\n");
}
if (error != -)
printf("\n\nerror at i==%d, a[i]==%d, a[i+1]==%d", error, a[error], a[error + ]); getchar();
return ;
}

● 输出结果(临时改为排序 64 个元素,每行显示 16个)

  , ,,,,,,, ,,,,,,,,
,,, ,,, ,,,,,,,,,,
,, ,,,,,,,, ,,,, ,,
,, ,,,,,,,,,,,,, , , , , , , , , , , , ,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,

OpenCL 双调排序 CPU 版的更多相关文章

  1. OpenCL 双调排序 GPU 版

    ▶ 参考书中的代码,写了 ● 代码,核函数文件包含三中算法 // kernel.cl __kernel void bitonicSort01(__global uint *data, const ui ...

  2. DirectX11 With Windows SDK--27 计算着色器:双调排序

    前言 上一章我们用一个比较简单的例子来尝试使用计算着色器,但是在看这一章内容之前,你还需要了解下面的内容: 章节 26 计算着色器:入门 深入理解与使用缓冲区资源(结构化缓冲区/有类型缓冲区) Vis ...

  3. 【转载】双调排序Bitonic Sort,适合并行计算的排序算法

    双调排序是data-independent的排序, 即比较顺序与数据无关的排序方法, 特别适合做并行计算,例如用GPU.fpga来计算. 1.双调序列 在了解双调排序算法之前,我们先来看看什么是双调序 ...

  4. 三十分钟理解:双调排序Bitonic Sort,适合并行计算的排序算法

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld 技术交流QQ群:433250724,欢迎对算法.技术.应用感兴趣的同学加入 双调排序是data-indepen ...

  5. tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案

    tensorflow安装过程cpu版-(windows10环境下)---亲试可行方案   一, 前言:本次安装tensorflow是基于Python的,安装Python的过程不做说明 二, 安装环境: ...

  6. Windows7下安装cpu版的Tensorflow

    windows7下安装python3.5 1.下载python-3.5.2-amd64.whl https://www.python.org/downloads/release/python-352/ ...

  7. 教你从头到尾利用DQN自动玩flappy bird(全程命令提示,GPU+CPU版)【转】

    转自:http://blog.csdn.net/v_JULY_v/article/details/52810219?locationNum=3&fps=1 目录(?)[-] 教你从头到尾利用D ...

  8. Ubuntu16.04下caffe CPU版的详细安装步骤

    一.caffe简介 Caffe,是一个兼具表达性.速度和思维模块化的深度学习框架. 由伯克利人工智能研究小组和伯克利视觉和学习中心开发. 虽然其内核是用C++编写的,但Caffe有Python和Mat ...

  9. CentOS 7 下使用虚拟环境Virtualenv安装Tensorflow cpu版记录

    1.首先安装pip-install 在使用centos7的软件包管理程序yum安装python-pip的时候会报一下错误: No package python-pip available. Error ...

随机推荐

  1. 实验吧—Web——WP之 头有点大

    3. 首先看一下 .net framework 9.9 并没有9.9版本 可以考虑浏览器伪装用户代理 就得了解User-Agent ???何为user-agent User-Agent是Http协议中 ...

  2. CountDownLatch的简单理解

    CountDownLatch的概念 CountDownLatch是一个同步工具类,用来协调多个线程之间的同步,或者说起到线程之间的通信(而不是用作互斥的作用). CountDownLatch能够使一个 ...

  3. c++hook全局触控事件

    https://gist.github.com/vbfox/1339671 namespace BlackFox { using System; using System.ComponentModel ...

  4. systemd学习笔记

    一.systemd介绍 systemd即为system daemon,是linux下的一种init软件与多数发行版使用的System V风格init相比,systemd采用了以下新技术: (1) 采用 ...

  5. BZOJ 1845三角形面积并

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1845 给定100个三角形,求三角形面积并. 戴神模板太可怕.直接调用函数秒掉.思路 ...

  6. 时间操作(JavaScript版)—页面显示格式:年月日 上午下午 时分秒 星期

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/wangshuxuncom/article/details/35222531 <!DOCTYPE ...

  7. php实现cookie加密解密

    1.加密解密类 class Mcrypt { /** * 解密 * * @param string $encryptedText 已加密字符串 * @param string $key 密钥 * @r ...

  8. Apache Spark 内存管理详解

    在spark里面,内存管理有两块组成,一部分是JVM的堆内内存(on-heap memory),这部分内存是通过spark dirver参数executor-memory以及spark.executo ...

  9. 1、搭建HBase完全分布式集群

    搭建完全分布式集群 HBase集群建立在hadoop集群基础之上,所以在搭建HBase集群之前需要把Hadoop集群搭建起来,并且要考虑二者的兼容性.现在就以5台机器为例,搭建一个简单的集群. 软件版 ...

  10. 关于WCF

    凡是被DataMember声明修饰的属性,必须要有get和set访问器,靠靠靠!!!! 给接口加 XmlSerializerFormat 强制用xml序列化.