Searching techniques
The search techniques allow you to locate an element of your choice from the list. The element to be search is compared with all the elements of the list and if a perfect match is found, then the element is retrieved and a message is displayed. Searching is a process of finding an element within the list of elements stored in any order or randomly.
Types of searching:
- Linear Searching
- Sequential Searching
- Binary Searching
1. Linear Searching: The linear search is a straight forward method of retrieving elements. This is often considered the simplest technique for searching an unordered list of elements. In Linear search we can access each element one by one till end. When an appropriate match is found, a message is displayed.
#include<stdio.h> #include<conio.h> void main() { int num[10], i, pos=-1, value; clrscr(); printf("Enter 10 numbers:"); for(i=0;i<10;i++) { scanf("%d",&num[i]); } printf("Enter number to be search:"); scanf("%d",&value); for(i=0;i<10;i++) { if(value==num[i]) { pos=i+1; break; } } if(pos==-1) { printf("\nThe element %d not found",value); } else { printf("\nThe position of %d is %d", value, pos); } getch(); }
Enter number to be search: 50
The position of 50 is 5
2. Sequential searching: In Sequential searching the list has to be arranged in a particular ascending order. The list is compared only till it is found or the is smaller than the number to be compared.
3. Binary Searching: Another simple method of accessing a list of elements is the binary search. Binary search is possible only if the list of elements is ordered and in an ascending manner. To sort the list of elements, any of the sorting techniques can be used. Once the list is ready, you can begin the binary search operation. Complexity of Binary Search is O (log2(n)).
In this method, a lower bound (lb) and an upper bound (ub) are assigned to the 1st and the last elements of the list. Once this is done, the "midpoint" of the list can be determined by finding the average of the lb and the ub. Once the midpoint is determined, the element to be searched is compared with the midpoint. If the element is matched, message is displayed and if the element is less than the midpoint, the rest of the search takes place with the elements on the left side of the midpoint. If the element is greater than the midpoint, then the comparison takes place with the elements on the right side of the midpoint.
#include<stdio.h> #include<conio.h> void main() { int num[10], i, beg, mid, end, pos=-1, value; clrscr(); printf("Enter 10 numbers:"); for(i=0;i<10;i++) { scanf("%d",&num[i]); } printf("Enter number to be search:"); scanf("%d",&value); beg = 0; end = 10-1; while(beg<=end) { mid=(beg+end)/2; if(value==num[mid]) { pos=mid+1; break; } else if(value >=num[mid]) { beg=mid+1; } else { end=mid-1; } } if(pos==-1) { printf("\nThe element %d not found",value); } else { printf("\nThe position of %d is %d", value, pos); } getch(); }
Enter number to be search: 50
The position of 50 is 5