It is used to
find out the location of the data item if it exists in the given collection of
data items.
Example: We have linear array A as below:
Suppose item to be searched is 20. We will start from beginning and will compare 20 with each element. This process will continue until element is found or array is finished. Here:
1) Compare 20 with 15
Output:
Example: We have linear array A as below:
1
|
2
|
3
|
4
|
5
|
15
|
50
|
35
|
20
|
25
|
Suppose item to be searched is 20. We will start from beginning and will compare 20 with each element. This process will continue until element is found or array is finished. Here:
1) Compare 20 with 15
20 # 15, go to next element.
2) Compare 20 with 50
20 # 50, go to next element.
3) Compare 20 with 35
20 #35, go to next element.
20 = 20, so 20 is found and its location is 4.
1A_Pro6: W.A.P. to perform searching operation on Array.
1A_Pro6: W.A.P. to perform searching operation on Array.
#include<stdio.h>
#include<conio.h>
void
main()
{
int array[10],i,key,n;
clrscr();
printf("\tEnter the number of
array elements = ",n);
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\tEnter
the %d element of array = ",i);
scanf("%d",&array[i]);
}
printf("\n\tEnter the element
to be searched:\t");
for(i=0;i<n;i++)
{
if(array[i]==key)
{
printf("\n\tThe
element is present at position %d",i+1);
}
}
if(i==n)
{
}
getch();
}
Output:
Deletion in 1-D Array | Sorting in 1-D Array |