▶ 学习了双调排序,参考(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. MySQL主从复制报错及解决方法

    mysql> show slave status \G *************************** 1. row *************************** Slave_ ...

  2. (dfs痕迹清理兄弟篇)bfs作用效果的后效性

    dfs通过递归将每种情景分割在不同的时空,但需要对每种情况对后续时空造成的痕迹进行清理(这是对全局变量而言的,对形式变量不需要清理(因为已经被分割在不同时空)) bfs由于不是利用递归则不能分割不同的 ...

  3. $.each()与$(selector).each()

    $.each()与$(selector).each()不同, 后者专用于jquery对象的遍历, 前者可用于遍历任何的集合(无论是数组或对象),如果是数组,回调函数每次传入数组的索引和对应的值(值亦可 ...

  4. PHP的extension_dir设置问题

    PHP安装时,extension_dir的路径要设成绝对路径:extension_dir = "D:/Tools/php-7.0.5/ext", 不然如果设成extension_d ...

  5. Eolinker API 接口文档神器

    Eolinker API 接口文档神器 群里小伙伴推荐的,还没有去研究,先记下来. API文档管理.自动化测试.开发协作利器 正在为数万企业管理超过100万APIs,提高开发效率以及规范开发流程

  6. mibox connections

    tcp        0      0 :::52549                :::*                    LISTEN      4398/net.myvst.v2:mt ...

  7. XFire构建web service客户端的五种方式

    这里并未涉及到JSR 181 Annotations 的相关应用,具体的三种方式如下 ① 通过WSDL地址来创建动态客户端 ② 通过服务端提供的接口来创建客户端 ③ 使用Ant通过WSDL文件来生成客 ...

  8. 【python】实例-用户登录系统

    有N,E,Q三个选择,若选择Q或者中断,则系统退出.若其他选项,则持续让用户选择. #!/usr/bin/env python db = {} def newuser(): prompt = 'log ...

  9. 在react-native中使用es7语法中的decorator装饰器

    在react-native中默认使用decorator会红屏报错,需要安装一个babel插件: babel-plugin-transform-decorators-legacy 然后在根目录下的.ba ...

  10. sql 查询年龄

    SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- ...