6字节数据 做 二分查找并插入排序

下列程序可以对 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 后面.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值