▶ 学习了双调排序,参考(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个)

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

最新文章

  1. 第K大数
  2. HDU 4280Island Transport(Dinc非STL 模板)
  3. Kernel Function--核函数收集
  4. jQuery 的append在ie下的兼容性
  5. (转)NoSQL系列:选择合适的数据库
  6. 深入学习微框架:Spring Boot
  7. 解决IIS网站.woff 404 (Not Found)问题
  8. CCF2014093字符串匹配(C语言版)
  9. JavaScript笔记之第六天
  10. asp.net C# 实现阿里大鱼和云片网短信接口类
  11. Linux 跟踪连接netfilter 调优
  12. nodejs 学习一 process.execPath 、 __dirname、process.cwd()的区别
  13. Cracking The Coding Interview 1.2
  14. C#中数据库事务、存储过程基本用法
  15. kong API gateway
  16. lock 默认公平锁还是非公平锁?公平锁是如何定义?如何实现
  17. pheanstalk put 延时队列
  18. Java Set集合(HashSet、TreeSet)
  19. iOS-app发布证书和调试证书配置
  20. NET Core中使用Apworks

热门文章

  1. django 远程数据库mysql migrate失败报error 1045之 解决方案
  2. hdu1255 覆盖的面积 线段树-扫描线
  3. LG1116 【车厢重组】
  4. linux日志分析
  5. mysql 存储过程知识点
  6. 电路交换vs分组交换
  7. Extjs下拉多选框
  8. $(function(){}) ,$(document).ready(function(){}),window.onload = function(){...},$(window).load(function(){...})区别
  9. 华为P10的内存门和闪存门的检测方法
  10. 利用Red Blob游戏介绍A*算法