---
title: "Java 中几种查找算法"
author: "zac110"
date: "2012-07-10T11:12:36Z"
tags: []
categories: ["行业应用"]
slug: "java"
draft: false
lastmod: "2012-07-10T11:12:36Z"
id: 1583890
url: "https://zac110.iteye.com/blog/1583890"
---

说明：顺序查找适合于存储结构为顺序存储或链接存储的线性表。   
int SequelSearch(elemtype s\[],keytype Key,int n)  
/\*在s\[0]-s\[n-1]中顺序查找关键字为Key的记录\*/  
/\*查找成功时返回该记录的下标序号；失败时返回-1\*/  
{  
int i;  
i=0;  
while(i&lt;n&&s\[i].Key!=Key)i++;

if(s\[i].Key==Key)return i;  
else return -1;   
}

－－－－－－－－－－－－－－－－－－－－－－－－－－－－  
二分查找

1、递归方法实现：  
int BSearch(elemtype a\[],elemtype x,int low,int high)  
/\*在下届为low，上界为high的数组a中折半查找数据元素x\*/  
{  
int mid;  
if(low&gt;high) return -1;  
mid=(low+high)/2;  
if(x==a\[mid]) return mid;  
if(x&lt;a\[mid]) return(BSearch(a,x,low,mid-1));  
else return(BSearch(a,x,mid+1,high));  
}

2、非递归方法实现：  
int BSearch(elemtype a\[],keytype key,int n)  
{  
int low,high,mid;  
low=0;high=n-1;  
while(low&lt;=high)   
   {  
      mid=(low+high)/2;  
      if(a\[mid].key==key) return mid;  
      else if(a\[mid].key&lt;key) low=mid+1;  
      else high=mid-1;  
   }  
return -1;  
}

－－－－－－－－－－－－－－－－－－－－－－－－－－  
分块查找

typedef int keytype;

typedef struct  
{  
keytype Key;  
}elemtype;

typedef struct  
{  
keytype Key;  
int Link;  
}indextype;

int IndexSequelSearch(indextype ls\[],elemtypes\[],int m,int l,keytype Key)  
/\*分块查找关键字为Key的记录。索引表为ls\[0]-ls\[m-1]\*/  
/\*顺序表为s，块长为l\*/  
{  
int i,j;  
/\*在索引表中顺序查找\*/  
i=0;  
while(i&lt;m&&Key&gt;ls\[i].Key)i++;

if(i&gt;=m)return -1;  
else  
{  
    /\*在顺序表中顺序查找\*/  
    j=ls\[i].Links;  
    while(Key!=s\[j].Key&&j-ls\[i].Link&lt;l)j++;

    if(Key==s\[j].Key)return j;  
    else return -1;   
}  
}

－－－－－－－－－－－－－－－－－－－－－－－－－－－－  
二叉排序树查找

1、二叉排序树查找算法：  
a、非递归算法：  
btree \*search(btree \*b,int x)  
/\*在二叉树b中查找x的过程\*/  
{  
if(b=NULL) return(NULL);  
else  
   {  
     if(b-&gt;data==x) return(b);  
     if(x&lt;b-&gt;data) return(search(b-&gt;left));  
     else return(search(b-&gt;right));  
   }   
}

b、递归算法：  
bsnodetype \*Search(bsnodetype \*bt,keytype Key)  
/\*在二叉树bt中查找元素为Key的元素\*/  
{  
bsnodetype \*p;  
if(bt==NULL) return(bt);

p=bt;  
while(p-&gt;Key!=Key)  
{  
    if(Key&lt;p-&gt;Key) p=p-&gt;Lchild;  
    else p=p-&gt;Rchild;  
    if(p==NULL)break;  
}  
return(p);  
}

2、二叉树的生成  
a、向一个二叉树b中插入一个结点s的函数如下：  
void insert(b,s)  
btree \*b,\*s;  
{  
if(b==NULL) b=s;  
else if(s-&gt;data==b-&gt;data)   
       return();  
else if(s-&gt;data&lt;b-&gt;data)  
       insert(b-&gt;left,s);  
else if(s-&gt;data&gt;b-&gt;data)  
       insert(b-&gt;right,s);  
}

b、生成二叉树  
void create(btree \*b)  
{  
int x;  
btree 8s;  
b==NULL;

do  
{  
   scanf("%d",&x);  
   s=(bnode \*)malloc(sizeof(bnode));  
   s-&gt;data=x;  
   s-&gt;left=NULL;  
   s-&gt;right=NULL;  
   insert(b,s);   
}while(x!=-1);  
}

c、从二叉树中删除一个结点

bsnodetype \*Delete(bsnodetype \*bt,keytype Key)  
/\*在bt为根结点的二叉树中删除值为Key的结点\*/  
{  
bsnodetype \*p,\*q;  
if(bt-&gt;Key==Key)   
{  
    /\*bt的左右子树均为空\*/  
    if(bt-&gt;Lchild==NULL&&bt-&gt;Rchild==NULL)  
     {  
       free(bt); /\*删除叶结点\*/  
       return(NULL);  
     }  
    else if(bt-&gt;Lchild==NULL)/\*bt的左子树为空\*/  
     {  
       p=bt-&gt;Rchild;  
       free(bt);  
       return(p);  
     }      
    else if(bt-&gt;Rchild==NULL)/\*bt的右子树为空\*/  
     {  
       p=bt-&gt;Lchild;  
       free(bt);  
       return(p);   
     }  
   else  
    {  
       p=q=bt-&gt;Rchild;  
       while(p-&gt;Lchild!=NULL)p=p-&gt;Lchild;  
       p-&gt;Lchild=bt-&gt;Lchild;  
       free(bt);  
       return(q);  
    }  
}

/\*在bt-&gt;Lchild为根结点的二叉树中删除值为Key的结点\*/  
if(bt-&gt;Key&gt;Key&&bt-&gt;Lchild!=NULL)  
   bt-&gt;Lchild=Delete(bt-&gt;Lchild,Key);

/\*在bt-&gt;Rchild为根结点的二叉树中删除值为Key的结点\*/  
if(bt-&gt;Key&lt;Key&&bt-&gt;Rchild!=NULL)  
   bt-&gt;Rchild=Delete(bt-&gt;Rchild,Key);

return(bt);  
}