Searching an Array
Write a C program to search for an element ‘a’ in the array. (Linear Search)
Input Format:
Input consists of n+2 integers. The first integer corresponds to ‘n’ , the size of the array. The next ‘n’ integers correspond to the elements in the array. The last integer corresponds to ‘a’, the element to be searched.
Assume that the maximum size of the array is 20.
Assume that the maximum size of the array is 20.
Output Format:
Refer sample output for details.
Sample Input 1:
5
2
3
6
8
1
6
Sample Output 1:
6 is present in the array
Sample Input 2:
5
2
3
6
8
1
9
Sample Output 2:
9 is not present in the array
Program
01 | #include<stdio.h> |
02 | int main(void) |
03 | { |
04 | int a[15],i,n,k,flag=0; |
05 | scanf("%d",&n); |
06 | for(i=0;i<n;i++) |
07 | scanf("%d",&a[i]); |
08 | scanf("%d",&k); |
09 | for(i=0;i<n;i++) |
10 | { |
11 | if(k==a[i]) |
12 | {flag=1; |
13 | break;} |
14 | } |
15 | if(flag==1) |
16 | printf("%d is present in the array",k); |
17 | else printf("%d is not present in the array",k); |
18 | return 0; |
19 | } |
Implementation of Binary Search
Write a C program to implement Binary Search Algorithm.
Include a function
int BinarySearch (int, int, int *, int x) --- The 1st parameter is the lower limit of the list or array, the 2nd parameter is the upper limit of the list or array, the third parameter is a pointer to the array and the fourth parameter is the search element.
Please note that the index of the search element is returned by the function. If the search element is not present in the array, -1 is returned.
Assume that the maximum size of the array is 10 . Please note that if a is the array, then a[0] is in position 0, a[1] is in position 1 ...
Input and Output Format:
Refer sample input and output for formatting specifications.
Sample Input and Output 1:
[All text in bold corresponds to input and the rest corresponds to output.]
Enter the number of elements :
5
Enter the elements :
12
16
23
45
67
Enter the element to be searched :
23
The element 23 is in position 2
5
Enter the elements :
12
16
23
45
67
Enter the element to be searched :
23
The element 23 is in position 2
Sample Input and Output 2:
[All text in bold corresponds to input and the rest corresponds to output.]
Enter the number of elements :
5
Enter the elements :
12
16
23
45
67
Enter the element to be searched :
28
The element 28 is not present in the array
5
Enter the elements :
12
16
23
45
67
Enter the element to be searched :
28
The element 28 is not present in the array
Function Definitions:
| int BinarySearch (int l, int h, int * a, int x) |
Program
01 | #include<stdio.h> |
02 | int BinarySearch(int l,int h,int *a,int x) |
03 | { |
04 | int pos=-1,mid; |
05 | mid=(l+h)/2; |
06 | while(l<=h) |
07 | { |
08 | if(x==a[mid]) |
09 | { pos=mid; |
10 | break;} |
11 | else if(a[mid]>x) |
12 | h=mid-1; |
13 | else if(a[mid]<x) |
14 | l=mid+1; |
15 | |
16 | mid=(l+h)/2; |
17 | } |
18 | if(pos!=-1) |
19 | return(pos); |
20 | else return -1; |
21 | } |
22 | int main(void) |
23 | { |
24 | int a[10],n,i,k; |
25 | printf("Enter the number of elements :"); |
26 | scanf("%d",&n); |
27 | printf("\nEnter the elements :"); |
28 | for(i=0;i<n;i++) |
29 | scanf("%d",&a[i]); |
30 | printf("\nEnter the element to be searched :"); |
31 | scanf("%d",&k); |
32 | |
33 | int pos=BinarySearch(0,n-1,a,k); |
34 | if(pos!=-1) |
35 | printf("\nThe element %d is in position %d",k,pos); |
36 | else printf("\nThe element %d is not present in the array",k); |
37 | return 0; |
38 | } |