BitonicSort
来源:互联网 发布:网络美术培训 编辑:程序博客网 时间:2024/06/02 02:06
// BitonicSort.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <math.h>// Max array lengthconst int MAX_LEN = 1024;// Cubic dimensionconst int DIMENSION = 10;// Sort orderenum SORT_ORDER{// from min to maxMIN_MAX,// from max to minMAX_MIN};// array elementsint elem[MAX_LEN];bool requireReverse(int a, int b, SORT_ORDER order);void bitMerge(int prefix, int N, SORT_ORDER order);void bitSort(int prefix,int N, SORT_ORDER order);void bitSplit(int prefix, int N, SORT_ORDER order);// Whether should reverse the pair a and b according to sort order// a: element on the left// b: element on the right// order: Sort orderbool requireReverse(int a, int b, SORT_ORDER order){bool result;switch(order){case MIN_MAX:result = a > b;break;case MAX_MIN:result = a < b;break;default:break;}return result;}// Bitonic merge// prefix: prefix of the dimension// N: current dimension to be sorted// order: Sort ordervoid bitMerge(int prefix, int N, SORT_ORDER order){if(N==0) return;bitMerge(prefix, N-1, MIN_MAX);bitMerge(prefix + (int)pow(2, N-1), N-1, MAX_MIN);bitSort(prefix, N, order);}// Sort elements in current dimension where elements are bitonic sequence// prefix: prefix of the dimension// N: current dimension to be sorted// order: Sort ordervoid bitSort(int prefix,int N, SORT_ORDER order){if (N==0) return;bitSplit(prefix, N, order);bitSort(prefix, N-1, order);bitSort(prefix + (int)pow(2, N-1), N-1, order);}// Split bitonic sequence into two bitonic sequences// prefix: prefix of the dimension// order: Sort ordervoid bitSplit(int prefix, int N, SORT_ORDER order){int i;int len = (int)pow(2, N-1);int prefixOther = prefix + (int)pow(2, N-1);for(i=0;i<len;i++){if(requireReverse(elem[prefix+i], elem[prefixOther+i], order)){int temp = elem[prefix+i];elem[prefix+i] = elem[prefixOther+i];elem[prefixOther+i] = temp;}}}int main(int argc, char* argv[]){int i;int len = (int)pow(2, DIMENSION);for(i=0;i<len;i++){elem[i] = len - i;}bitMerge(0, DIMENSION, MIN_MAX);for(i=0;i<len;i++){printf("%d ", elem[i]);}return 0;}
0 0
- BitonicSort
- 串行的BitonicSort双调排序
- AMD-SDK的学习[5]--BitonicSort
- 迟到的年终总结,2012-2014
- Crazy Tetris 01_基本设置&主菜单场景
- zoj1423(Your)((Term)((Project))) (栈——基础练习)
- 九度 题目1548:平面上的点
- Android数据库LitePal的存储操作
- BitonicSort
- 1007 素数对猜想
- ContentProvider
- 1008 数组元素循环右移问题
- 生成Webservice客户端的4中方法
- 七周七语言之Haskell代码学习
- Genymotion安装
- 2015 年到了
- 2014回顾与展望2015