#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <iostream>
using namespace std;
//***copy from (http://blog.youkuaiyun.com/here1009/article/details/7918185)
class bitonic_sorter{
public:
bitonic_sorter(float a[], int len);
void sort(bool dir=true);
void sort_for_arbitray_length(bool dir=true);
private:
float* array;//change
int length;
void bitonic_sort(int lowbundary, int len,bool direction );
void bitonic_sort_for_arbitrary_length(int lowbundary,int len,bool direction);
void bitonic_merge(int lowbundary,int len,bool direction);
void bitonic_merge_for_arbitrary_length(int lowbundary,int len,bool direction);
void compare_and_swap(int i,int j,bool direction);
int greatest_power_of_2_lessthan(int len);
};
bitonic_sorter::bitonic_sorter(float a[],int len){
array=a;
length=len;
}
void bitonic_sorter::sort(bool direction){
bitonic_sort(0,length,direction);
}
void bitonic_sorter::sort_for_arbitray_length(bool direction){
bitonic_sort_for_arbitrary_length(0,length,direction);
}
void bitonic_sorter::bitonic_sort(int lowbundary,int len,bool direction){
if(len>1){
int m=len/2;
bitonic_sort(lowbundary,m,direction);
bitonic_sort(lowbundary+m,m,!direction);
bitonic_merge(lowbundary,len,direction);
}
}
void bitonic_sorter::bitonic_sort_for_arbitrary_length(int lowbundary,int len,bool direction){
if(len>1){
int m=len/2;
if(direction==true){
bitonic_sort(lowbundary,m,!direction);
bitonic_sort(lowbundary+m,len-m,direction);
写一个分段双调排序算法
最新推荐文章于 2024-06-04 20:51:11 发布