Searching technique refers to finding a key element among the list of elements.
If the given element is present in the list, then the searching process is said to be successful.
If the given element is not present in the list, then the searching process is said to be unsuccessful.
C language provides two types of searching techniques. They are as follows −
- Linear search
- Binary search
Linear Search
- Searching for the key element is done in a linear fashion.
- It is the simplest searching technique.
- It does not expect the list to be sorted.
- Limitation − It consumes more time and reduce the power of system.
Input (i/p)
Unsorted list of elements, key.
Output (o/p)
- Success − If key is found.
- Unsuccessful − Otherwise.
Example
Following is the C program for linear searching technique −
#include<stdio.h> int main (){ int a[50], n, i, key, flag = 0; printf("enter the no: of elements"); scanf ("%d",&n); printf("enter the elements:\n"); for (i=0; i<n; i++) scanf( "%d", &a[i]); printf("enter a key element:\n"); scanf ("%d", &key); for (i=0; i<n; i++){ if (a[i] == key){ flag = 1; break; } } if (flag == 1) printf("search is successful:"); else printf("search is unsuccessfull:"); return 0; }
Output
When the above program is executed, it produces the following result −
enter the no: of elements5 enter the elements:12 45 13 67 78 enter a key element:67 search is successful: