How to use binary search on 2 tables of sorted data in c
I am looking into how to use binary search in the c language.
I will begin with,
i have a struct
struct keyval{
unsigned short key[2];
unsigned short val[2];
};
as you can see this is a struct with 2 arrays of shorts whic开发者_JAVA技巧h can hold 2 short values.
i then memory allocate to these structs. this enables me to store many of this type of struct in memory.
struct keyval *codes1 = malloc(sizeof(struct keyval)*(0x1000000));
struct keyval *codes2 = malloc(sizeof(struct keyval)*(0x1000000));
I go through some loops and populate codes1 with struct keyval's. and then in another loop i populate codes2 with struct keyval's also
this data will have been populated on the basis of an iterating key. so basically it will be automatically sorted by key value.
i then sort my data on the val
value . using the c qsort function
and my own compare function
if the short in location 0 of the val array of one is less than the short in the same position of the other struct then return -1 and so on.
qsort(codes1,SIZE_OF_CODES1, sizeof(struct keyval), compare);
qsort(codes2,SIZE_OF_CODES2, sizeof(struct keyval), compare);
int compare(const void *a, const void *b){
struct keyval *ia = (struct keyval *)a;
struct keyval *ib = (struct keyval *)b;
if(ia->val[0] > ib->val[0]){
return 1;
}else if (ia->val[0] < ib->val[0]){
return -1;
}else{
return (ia->val[1] - ib->val[1]);
}
}
now this is where i am stuck
i need to be able to binary search for every value in codes1 against the values in codes2.
and when codes1[i].val == codes2[i].val
i can print out the values of codes1[i].key
and then codes2.[i].key
my attempt is as such
while(e <= SIZE_OF_CODES1){
struct keyval *found = (struct keyval*) bsearch(&codes1[e],codes2,SIZE_OF_CODES2,sizeof(struct keyval),compare );
if(found){
printf("\n found key = %08x %04x value = %04x %04x", found -> key, found -> key[1],found->val[0],found ->val[1] );
}
e++;
}
the above code will not find if the data matches as, i am certain there are places in the code where it matches
//added edit
just did a debug, and i found that when the bsearch calls the compare function,everytime it is called all the values associated with ia are zero but ib is correct.
the sort method is correct
could someone point me in the right direction on how to implement a binary search , i am getting very stuck
my old compare function
int compare(const void *a, const void *b){
struct keyval *ia = (struct keyval *)a;
struct keyval *ib = (struct keyval *)b;
if(ia->val[0] > ib->val[0]){
return 1;
}else if(ia->val[0] < ib->val[0]){
return -1;
}else{
if(ia->val[1] > ib->val[1]){
return 1;
}else if(ia->val[1] < ib->val[1]){
return -1;
}else{
return 0;
}
}
}
to compare the lists as you describe, step through codes1, then do a bsearch() on codes2 for each entry in codes1. You can reuse compare. I am not sure if SIZE_OF_CODES1 and 2 are variables or defines, I am assuming they reflect the count of elements in each array.
struct keyval *src=codes1;
struct keyval *dest=code2;
struct keyval *p
int i=0;
while(i< SIZE_OF_CODES1)
{
if( (p=(struct keyval *)bsearch(src, dest, SIZE_OF_CODES2, sizeof(struct keyval), compare))!=NULL)
printf("duplicate codes1: %d %d codes2: %d %d\n", src->key,src->val, p->key, p->val);
i++
}
精彩评论