#include
int search(int key, int a[], int len)
{
int ret=-1;
for(int i=0;i
散列表 hash-table
#include
int amount[]={1,5,10,25,50};
char *name[]={"penny","nickel","dime","quarter","half-dollar"};
int search(int key, int a[], int len)
{
int ret=-1;
for(int i=0;i-1){
// printf("%s\n",name[r]);
//}
printf("%s\n",name[r]);
return 0;
}
两个数组联合到一起--采用struct结构体的方式
#include
//int amount[]={1,5,10,25,50};
//char *name[]={"penny","nickel","dime","quarter","half-dollar"};
struct{
int amount;
char *name;
}coins[]={
{
1,"penny"
},
{
5,"nickel"
},
{
10,"dime"
},
{
25,"quarter"
},
{
50,"half-dollar"
}
};
int search(int key, int a[], int len)
{
int ret=-1;
for(int i=0;i
二分搜索
log2n
int search(int key, int a[], int len)
{
int left=0;
int right=len-1;
int ret=-1;
while(right>left){
int mid=(left+right)/2;
if(key==a[mid])
{
ret=mid;
break;
}
else if(a[mid]>key)
{
right=mid-1;
}
else
{
left=mid+1;
}
}
return ret;
}