gtest介绍及测试用例如下:测试框架之GTest
MIT《算法导论》下载:hereorhttp://download.youkuaiyun.com/detail/ceofit/4212385
源码下载:here orhttp://download.youkuaiyun.com/detail/ceofit/4218488
定义基本的交换类:后续介绍几个排序算法,作一些准备工作。
重定义基本类型及宏:
#ifndef NULL
#define NULL 0
#endif
typedef unsigned char byte;
typedef unsigned long long uint64;
typedef long long int64;
typedef unsigned long uint32;
typedef long int32;
typedef unsigned short uint16;
typedef short int16;
typedef unsigned char uint8;
typedef char int8;
定义排序函数的统一格式,例如选择法排序:
void SelectionSort(int *pArray,int cnt);
定义排序函数指针类型SK_SORT:
typedef void(*SK_SORT)(int *pArray,int cnt);
定义几个基本函数:
获取在min与max之间的随机数,要求max-min<65535:
int get_random(int min,int max)
{
static bool bget_random_init = true;
if(bget_random_init)
{
srand(time(0));
bget_random_init = false;
}
return min + rand() % (max - min);
}
获取数组最大最小值:
int get_max(int *pArray,int cnt)
{
if(cnt <= 0)
return 0;
int max = pArray[0];
int i = 0;
for(i=1; i<cnt; i++)
{
max = max>=pArray[i] ? max : pArray[i];
}
return max;
}
int get_min(int *pArray,int cnt)
{
if(cnt < 0)
return 0;
int min = pArray[0];
int i = 0;
for(i=1; i<cnt; i++)
{
min = min<=pArray[i] ? min : pArray[i];
}
return min;
}
获取数值位数:
int get_bitcnt(int n)
{
int cnt=0;
if(((n>>31) & 0x01) == 1)
return 32;
while(n)
{
cnt++;
n=n>>1;
}
return cnt;
}
定义基本的交换类:
inline void exchange(int *x,int *y)
{
int t = *x;
*x = *y;
*y = t;
}
测试使用gtest测试框架
有几点注意:
1、使用基本int类型,因为主要学习算法,所以算法并没考虑做到通用,没有使用模板或void*类型。
2、排序默认都是从小到大。