Two search methods in C Language

Source: Internet
Author: User

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};  // Unordered Sequence
07     int
S[11]={0,10,20,30,35,40,65,67,69,84,97};// Ordered sequence
08     printf("The position in the L where element 68 is located is % d",seqSearch(L,68));
09     // Printf ("the position of element 67 in S is % d", binsearch (S, 67 ));
10     return
0;
11 }
12 int
seqSearch(
int
L[11],
int key) 
// Sequential search
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)
// Half-Lookup
23 {
24     int
low=1,high=10,m=0;
25     while(low<=high)
26     {
27         m=(low+high)/2;  
// Half fold
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 }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.