本文共 1128 字,大约阅读时间需要 3 分钟。
- public class seqSearch {
-
-
-
-
-
-
-
-
-
-
-
-
-
- int SeqSearch(int r[],int n,int k)
- {
- int i=0;
- while(i<n&&r[i]!=k)
- i++;
- if(i<n)
- return i;
- else
- return -1;
- }
-
-
-
- int SeqSearch2(int r[],int n,int k)
- {
- int i=0;
- r[n-1]=k;
- while(r[i]!=k)
- i++;
- if(i<n)
- return i;
- else
- return -1;
- }
- public static void main(String[] args) {
- seqSearch sq=new seqSearch();
- int r[]={ 1,2,3,4,5};
- System.out.println(sq.SeqSearch(r,5,3));
- System.out.println(sq.SeqSearch2(r,5,3));
-
- }
-
- }
public class seqSearch { /** * @param args */ /* 顺序查找又称线性查找; 基本思想:从查找表的一端开始,向另一端逐个按给定值K与关键字进行比较,若找到,查找成功; 并给出记录在表中的位置;若整个表检测完,仍未找到与K值相同的关键字,则查找失败; 优点:对表中数据的存储没有要求,对于链表,只能进行顺序查找; 缺点:当n值很大时,平均查找长度较大,效率低; */ //无监视哨的情况,查询成功返回该对象的下标序号,失败时返回-1。 int SeqSearch(int r[],int n,int k) { int i=0; while(i
转载于:https://blog.51cto.com/daheyuan/1140091