In computer science, a search allows the efficient retrieval of specific items from a set of items, such as a specific record from a database.
The simplest, most general, and least efficient search structure is merely an unordered sequential list of all the items. Locating the desired item in such a list, by the linear search method, inevitably requires a number of operations proportional to the number n of items, in the worst case as well as in the average case. Useful search data structures allow faster retrieval; however, they are limited to queries of some specific kind. Moreover, since the cost of building such structures is at least proportional to n, they only pay off if several queries are to be performed on the same database (or on a database that changes little between queries).
Output |
#include <stdio.h>
#include <conio.h>
int main()
{
int loc, n, data, x, a[100];
printf("Enter no. of values: ");
scanf("%d", &n);
printf("Enter %d values: ", n);
for(x=0; x<n; x++) {
scanf("%d", &a[x]);
}
printf("Entered array is: \n");
for(x=0; x<n; x++) {
printf("%d ", a[x]);
}
printf("\nEnter data to search: ");
scanf("%d", &data);
for(x=0; x<n; x++) {
if(data==a[x]) {
printf("Location is: %d\n", x);
loc=x;
break;
}
loc=x;
}
if(data!=a[loc]) {
printf("Element not found\n");
}
return(0);
}
Read More >>