01 | #include <stdio.h> |
02 | #include <stdlib.h> |
03 |
04 | int main() |
05 | { |
06 | int L[11]={0,70,38,65,97,76,93,1,15,68,64}; //无序序列 |
07 | int S[11]={0,10,20,30,35,40,65,67,69,84,97}; //有序序列 |
08 | printf ( "元素68所在L中的位置为%d " ,seqSearch(L,68)); |
09 | //printf("元素67所在S中的位置为%d ",binSearch(S,67)); |
10 | return 0; |
11 | } |
12 | int seqSearch( int L[11], int key) //顺序查找 |
13 | { |
14 | int i=0; |
15 | for (i=10;i>=0;i--) |
16 | { |
17 | if (L[i]==key) |
18 | return i; |
19 | } |
20 | return 0; |
21 | } |
22 | int binSearch( int S[11], int key) //折半查找 |
23 | { |
24 | int low=1,high=10,m=0; |
25 | while (low<=high) |
26 | { |
27 | m=(low+high)/2; //折半 |
28 | if (S[m]==key) |
29 | return m; |
30 | if (key<S[m]) |
31 | high=m-1; |
32 | else |
33 | low=m+1; |
34 | } |
35 | return 0; |
36 | } |