bsearch&qsort
void print(char (*arr)[10],int len)
{
int i;
for (i=0;i<len;i++)
{
printf("%s ",arr[i]);
}
printf("\n");
}
int main()
{
char arr[][10]={"bac","bca","abc","acb","cba","cab"};
char *key="bca";
char *ptr=NULL;
printf("before qsort :");
print(arr,LENGTH(arr));
qsort((void *)arr,LENGTH(arr),sizeof(arr[0]),(int (*)(const void *,const void *))strcmp);
printf("after qsort :");
print(arr,LENGTH(arr));
ptr=(char *)bsearch(key,arr,LENGTH(arr),sizeof(arr[0]),(int (*)(const void *,const void *))strcmp);
if (ptr)
{ /* 找到*/
printf("%s is in the array\n",key);
}
else/* 没找到*/
{
printf("%s isn't in the array\n",key);
}
}
{
int i;
for (i=0;i<len;i++)
{
printf("%s ",arr[i]);
}
printf("\n");
}
int main()
{
char arr[][10]={"bac","bca","abc","acb","cba","cab"};
char *key="bca";
char *ptr=NULL;
printf("before qsort :");
print(arr,LENGTH(arr));
qsort((void *)arr,LENGTH(arr),sizeof(arr[0]),(int (*)(const void *,const void *))strcmp);
printf("after qsort :");
print(arr,LENGTH(arr));
ptr=(char *)bsearch(key,arr,LENGTH(arr),sizeof(arr[0]),(int (*)(const void *,const void *))strcmp);
if (ptr)
{ /* 找到*/
printf("%s is in the array\n",key);
}
else/* 没找到*/
{
printf("%s isn't in the array\n",key);
}
}