下列程序可以对 6字节数据 进行二分查找,查找成功返回找到的位置,查找失败返回应该插入的位置,并将新数据插入到有序数据中.
test.c
unsigned long myBinSearch(unsigned char *R, int n , unsigned char *K, unsigned long *index)
{
long low=0;
long high = n-1;
long mid = 0;
unsigned char tmpData[6];
memcpy(tmpData, &R[low * 6], 6);
if(memcmp(tmpData, K, 6) == 0)
{
*index = 0;
return 0 ;
}
while(low <= high)
{
mid = low + ((high - low) / 2);
memcpy(tmpData, &R[mid * 6], 6);
if(memcmp(tmpData, K, 6) == 0)
{
*index = mid;
return 0;
}
memcpy(tmpData, &R[mid * 6], 6);
if(memcmp(tmpData, K, 6) > 0)
{
high = mid - 1;
}
else
{
low = mid + 1;
}
}
memcpy(tmpData, &R[mid * 6], 6);
if(memcmp(tmpData, K, 6) > 0)
{
*index = mid;
}
else if(memcmp(tmpData, K, 6) < 0)
{
*index = mid + 1;
}
return -1;
}
void main(int argc, char **argv)
{
unsigned long index = 0;
int ret = 0;
int i = 0;
int j = 0;
int id = 0;
unsigned char arr[54]= { 00, 00, 44, 66, 66, 60,
00, 00, 44, 66, 66, 63,
00, 00, 44, 66, 66, 68,
00, 00, 44, 66, 66, 71,
00, 00, 44, 66, 66, 75,
00, 00, 44, 66, 66, 84,
00, 00, 44, 66, 66, 93,
00, 00, 44, 66, 66, 98};
unsigned char buf1[6] = {00, 00, 44, 66, 66, 51,};
unsigned char buf2[6] = {00, 00, 44, 66, 66, 62,};
unsigned char buf3[6] = {00, 00, 44, 66, 66, 67,};
unsigned char buf4[6] = {00, 00, 44, 66, 66, 73,};
unsigned char buf5[6] = {00, 00, 44, 66, 66, 77,};
unsigned char buf6[6] = {00, 00, 44, 66, 66, 81,};
unsigned char buf7[6] = {00, 00, 44, 66, 66, 90,};
unsigned char buf8[6] = {00, 00, 44, 66, 66, 97,};
unsigned char buf9[6] = {00, 00, 44, 66, 66, 99,};
unsigned char tmpBuf[6];
id = atoi(argv[1]);
switch(id)
{
case 1:
memcpy(tmpBuf, buf1, 6);
break;
case 2:
memcpy(tmpBuf, buf2, 6);
break;
case 3:
memcpy(tmpBuf, buf3, 6);
break;
case 4:
memcpy(tmpBuf, buf4, 6);
break;
case 5:
memcpy(tmpBuf, buf5, 6);
break;
case 6:
memcpy(tmpBuf, buf6, 6);
break;
case 7:
memcpy(tmpBuf, buf7, 6);
break;
case 8:
memcpy(tmpBuf, buf8, 6);
break;
case 9:
memcpy(tmpBuf, buf9, 6);
break;
default:
memcpy(tmpBuf, buf1, 6);
break;
}
for(i = 0; i < 48; i++)
{
printf("%d ", arr[i]);
if((i+1) % 6 == 0)
{
printf("\n");
}
}
ret = myBinSearch((unsigned char *)arr, 8, tmpBuf, &index);
printf("ret = %d \n", ret);
if (ret == 0)
{
printf("find %d \n", index);
}
else if(ret == -1)
{
printf("must insert in index = %d \n", index);
memcpy(&arr[6 * (index + 1)], &arr[6 * index], sizeof(arr) - 6 * index);
memcpy(&arr[6 * index], tmpBuf, 6);
for(i = 0; i < 54; i++)
{
printf("%d ", arr[i]);
if((i+1) % 6 == 0)
{
printf("\n");
}
}
}
}
运行:
./test 1
0 0 44 66 66 60
0 0 44 66 66 63
0 0 44 66 66 68
0 0 44 66 66 71
0 0 44 66 66 75
0 0 44 66 66 84
0 0 44 66 66 93
0 0 44 66 66 98
ret = -1
must insert in index = 0
0 0 44 66 66 51
0 0 44 66 66 60
0 0 44 66 66 63
0 0 44 66 66 68
0 0 44 66 66 71
0 0 44 66 66 75
0 0 44 66 66 84
0 0 44 66 66 93
0 0 44 66 66 98
表示查找失败,并把 0 0 44 66 66 51 插入到 有序数据最前面.
./test 4
0 0 44 66 66 60
0 0 44 66 66 63
0 0 44 66 66 68
0 0 44 66 66 71
0 0 44 66 66 75
0 0 44 66 66 84
0 0 44 66 66 93
0 0 44 66 66 98
ret = -1
must insert in index = 4
0 0 44 66 66 60
0 0 44 66 66 63
0 0 44 66 66 68
0 0 44 66 66 71
0 0 44 66 66 73
0 0 44 66 66 75
0 0 44 66 66 84
0 0 44 66 66 93
0 0 44 66 66 98
表示查找失败,并把 0 0 44 66 66 73 插入到 有序数据的下标为4的地方,即0 0 44 66 66 71 后面.