DS Unit 5
DS Unit 5
DS Unit 5
UNIT-5
Now a days with increasing of technology and availability of data and management of
large data bases makes the searching task can be most challenging issue to retrieve for
particular resource.
Search is a process of finding a value in a list of values. In other words, searching is the
process of locating given value position in a list of values according to or based on user
requirement.
This search process starts comparing of search element with the first element in the list.
If both are matching then results with element found otherwise search element is
compared with next element in the list.
Otherwise, repeat the same with the next element in the list until search element is
compared with last element in the list, if that last element also doesn't match, then the
result is "Element not found in the list".
That means, the search element is compared with element by element in the list.
Step 2: Compare, the search element with the first element in the list.
Step 3: If both are matching, then display "Given element found!!!" and terminate the
function
Step 4: If both are not matching, then compare search element with the next element in the
list.
Step 5: Repeat steps 3 and 4 until the search element is compared with the last element in the
list.
Step 6: If the last element in the list is also doesn't match, then display "Element not found
and terminate the function.
Example
void main(){
int list[20],size,i,sElement;
Output:
Enter the size
4
Enter the values
10 20 30 40
Enter search element
20
Element is found at 1 index
Binary search:
Binary search algorithm finds given element in a list of elements from n number of
elements in the list.
The binary search algorithm can be used with only sorted list of element. That means,
binary search can be used only with lkist of element which are already arranged in a
order.
The binary search cannot be used for list of element which are in random order. This
search process starts comparing of the search element with the middle element in the list.
If both are matched, then the result is "element found". Otherwise, we check whether the
search element is smaller or larger than the middle element in the list.
If the search element is smaller, then we repeat the same process for left sublist of the
middle element.
If the search element is larger, then we repeat the same process for right sublist of the
middle element.
We repeat this process until we find the search element in the list or until we left with a
sublist of only one element.
And if that element also doesn't match with the search element, then the result is
"Element not found in the list".
Example
Consider the following list of element and search element...
void main()
{
int first, last, middle, size, i, sElement, list[100];
clrscr();
first = 0;
last = size - 1;
middle = (first+last)/2;
Output:
Enter the size
5
Enter the values
10 20 30 40 50
Enter search element
30
Element is found at 2 index