0% found this document useful (0 votes)
13 views10 pages

Searching

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views10 pages

Searching

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT 4

Searching Techniques

Linear Search Algorithm(Sequential Search)


What is Search?

Search is a process of finding a value in a list of values. In other words, searching


is the process of locating a given value position in a list of values.

Linear Search Algorithm (Sequential Search Algorithm)

• Linear search algorithm finds a given element in a list of elements with O(n)
time complexity where n is the total number of elements in the list.
• This search process starts comparing the search element with the first element in
the list.
• If both are matching then results with the element found otherwise search
element is compared with the next
element in the list.
• If both are matched, then the result is "element found". Otherwise, repeat the
same with the next element in the list until the search element is compared with
the 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.
Linear search is implemented using the following steps...

Step 1: Read the search element from the user

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 the 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 also doesn't match, then display "Element
not found!!!" and terminate the function.

Consider the following list of elements and search element...


Program:

#include<stdio.h>

void main(){
int list[20], size ,i, sElement;

printf("Enter size of the list: ");


scanf("%d",&size);

printf("Enter any %d integer values: ",size);

for(i = 0; i < size; i++)


scanf("%d",&list[i]);

printf("Enter the element to be Search: ");

scanf("%d",&sElement);

// Linear Search Logic


for(i = 0; i < size; i++)
{
if(sElement == list[i])
{
printf("Element is found at %d index", i);
break;
}
}
if(i == size)
printf("Given element is not found in the list!!!");
}

Output:

Enter size of the list: 10


Enter any 10 integer values: 12 50 63 23 56 89 45 78 30 52
Enter the element to be Search: 56
Element is found at 4 index
Binary Search Algorithm

What is Search?
Search is a process of finding a value in a list of values. In other words, searching
is the process of locating a given value position in a list of values.
Binary Search Algorithm

• Binary search algorithm finds a given element in a list of elements with O(log n)
time complexity where n is total number of elements in the list.
• The binary search algorithm can be used with only a sorted list of element.
• That means, binary search can be used only with a list of element which are
already arranged in a order.
• The binary search can not be used for lists of element which are in random
order.
• This search process starts comparing 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 the left
sublist of the middle element.
• If the search element is larger, then we repeat the same process for the right
sublist of the middle element.
• We repeat this process until we find the search element in the list or until we are
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".

Binary search is implemented using the following steps...

Step 1: Read the search element from the user


Step 2: Find the middle element in the sorted list

Step 3: Compare the search element with the middle element in the sorted list.
Step 4: If both are matching, then display "Given element found!!!" and
terminate the function
Step 5: If both are not matching, then check whether the search element is smaller
or larger than the middle element.
Step 6: If the search element is smaller than the middle element, then repeat
steps 2, 3, 4 and 5 for the left sublist of the middle element.
Step 7: If the search element is larger than the middle element, then repeat steps 2,
3, 4 and 5 for the right sublist of the middle element.
Step 8: Repeat the same process until we find the search element in the list or
until the sublist contains only one element.
Step 9: If that element also doesn't match with the search element, then display
"Element not found in the list!!!" and terminate the function.

Example
Consider the following list of elements and search element...
Program:

#include<stdio.h>

void main()
{
int first, last, middle, size, i, sElement,
list[100]; clrscr();

printf("Enter the size of the list: ");


scanf("%d",&size);

printf("Enter %d integer values in Ascending order\n", size);

for (i = 0; i < size; i++)


scanf("%d",&list[i]);

printf("Enter value to be search: ");


scanf("%d", &sElement);

first = 0;
last = size - 1;
middle = (first+last)/2;

while (first <= last) {


if (list[middle] < sElement)
first = middle + 1;
else if (list[middle] == sElement) {
printf("Element found at index
%d.\n",middle); break;
}
else
last = middle - 1;

middle = (first + last)/2;


}
if (first > last)
printf("Element Not found in the list.");

}
Output:

Enter the size of the list: 6

Enter 6 integer values in Ascending order

10 23 45 68 79 93

Enter value to be search: 40

Element Not found in the list.

You might also like