编写无序顺序表顺序查找、有序顺序表顺序查找、二分查找算法。用c语言。高分急求!

2025-03-23 02:16:49
推荐回答(1个)
回答1:

int IdxSerch(SeqList A[],IdxType index[],int b,KeyType k,int n) {
//分块查找关键字为k的记录,索引表为
index[0..b-1]
int low=0,high=b-1,mid,i;
int s=n/b; //每块记录个数
while(low<=high)
{
//在索引表中进行二分查找,找到的位置放在low中
mid=(low+high)/2;
if(index[mid].key else high=mid-1;
}

if(low {
//在顺序表中顺序查找
for(i=index[low].link;i<=index[low].link+s-1 && i if(A[i].key==k) return i;
return -1;
}
return -1;
}

typedef struct node
{
KeyType key;
//结点中的关键字
struct node *lchild,*rchild; //左、右孩子指针
}BsTree;

BsTree *BstSeareh(BsTree *BST,KeyType k ,BsTree **parent)
{
BsTree *p=BST,*q=NULL; //p指向根结点,q指向*p的双亲
while(p!=NULL)
{
if(k==p->key)
{ //查找成功
*parent=q;
return (p);
}
q=p;
if(kkey) p=p->lchild;
//在左子树中查找
else p=p->rchild; //在右子树中查找
}
*parent=q;
return (p);
//查找失败,返回空
}