Professional Documents
Culture Documents
Given an array arr[] of n elements, write a program to search a given element x in arr[].
Examples :
x = 110;
Output : 6
50,
x = 175;
Output : -1
AIM:
Data type:integer
Routine:iterative
PSEUDOCODE:
BEGIN
int arr[100],key,i, n;
for i= 0;
for i=0;
BEGIN
if (arr[i] ==key)
END
if (i== n)
return 0;
END
SOURCE CODE:
#include <stdio.h>
int main()
int arr[100],key,i, n;
scanf("%d", &n);
scanf("%d", &arr[i]);
scanf("%d", &key);
for (i=0;i<n;i++)
if (arr[i] ==key)
break;
if (i== n)
return 0;
}
OUTPUT:
RESULT:
By using the above code to search a given element x in arr[] using linear search is successfully
done and output is verified.
QUESTION:
EX:NO:3.3
DATE:26.11.2021 BINARY SEARCH
AIM:
Data type:integer
Routine:iterative
PSEUDOCODE:
BEGIN
for l= 0;
r=n - 1;
m=(l+r)/2;
while (l<=r) {
if (arr[m]<key)
l=m+1;
break;
else
r= m-1;
m=(l+r/2);
if (l>r)
END
return 0;
SOURCE CODE:
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &arr[i]);
scanf("%d", &key);
l= 0;
r=n - 1;
m=(l+r)/2;
while (l<=r) {
if (arr[m]<key)
l=m+1;
break;
else
r= m-1;
m=(l+r/2);
if (l>r)
return 0;
OUTPUT:
RESULT:
By using the above code to search a given element x in arr[] using linear search is successfully
done and output is verified.