1.二分查找
public int BinSearch(SeqList<int> R, int Key)
{二分查找
int low = 0, high = R.GetLength() - 1, mid;//置当前查找区间上、下界的初值
while (low <= high)
{ //当前查找区间R[low..high]非空
mid = (low + high) / 2;
if (R.Data[mid] == Key) return mid; //查找成功返回
if (R.Data[mid] > Key)
high = mid - 1; //继续在R[low..mid-1]中查找
else
low = mid + 1; //继续在R[mid+1..high]中查找
}
return -1; //当low>high时表示查找区间为空,查找失败
}
2.编程实现strcmp
int MyStrcmp (const char *src, const char *dst)
{
int ret = 0 ;
while( !(ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
{
++src;
++dst;
}
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
3.函数实现判断处理器是大端还是小端,若cpu是Big_endian,返回0;若是Little_endian,返回-1
//小端数据存放从低字节到高字节,大端数据存放从高字节到低字节 int checkCPU( ) {
union w { int a; char b; } c; c.a = 1;
return(c.b ==1);//c.b == 1为小段 }