查找-顺序表查找

a为数组,n为要查找的数组个数,key为要查找的关键字.

顺序查找又称为线性查找:从表中第一个或者最后一个记录开始,逐个进行记录的关键字和给定值比较,若某个记录的关键字和给定值相等,则查找成功,找到所查记录;如果直到最后一个(或第一个)记录,其关键字和给定值比较都不等的时候.则表中没有所查的记录,查找不成功.

 1 #include "stdafx.h"
 2 #include <iostream>
 3 #include <exception>
 4 #include<string>
 5 using namespace std;
 6 
 7 
 8 //顺序表查找的两种方法
 9 int Sequential_Search(int *a,int n,int key)
10 {
11     for(int i = 0;i!=n;i++)
12     {
13         if (key==a[i])
14             return i;
15     }
16     return 0 ;
17 }
18 
19 int Sequential_Search2(int *a,int n,int key)
20 {
21     int i;
22     a[0]=key;
23     i = n;
24     while(a[i]!=key)
25     {
26         i--;
27     }
28     return i;
29 }
30 int _tmain(int argc, _TCHAR* argv[])
31 { 
32     
33 
34     return 0 ;
35 }
原文地址:https://www.cnblogs.com/crazycodehzp/p/3550577.html