0% found this document useful (0 votes)
61 views4 pages

Chapter 2 - Searching: Linear Search

The document summarizes linear and binary search algorithms. Linear search compares the search element to every element in the list sequentially until a match is found. Binary search works only on sorted lists by comparing the search element to the middle element of the list and recursively searching either the upper or lower half. Binary search requires fewer comparisons than linear search, especially for large lists, but the list must be sorted for binary search to work.

Uploaded by

Tanvi Chiman
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)
61 views4 pages

Chapter 2 - Searching: Linear Search

The document summarizes linear and binary search algorithms. Linear search compares the search element to every element in the list sequentially until a match is found. Binary search works only on sorted lists by comparing the search element to the middle element of the list and recursively searching either the upper or lower half. Binary search requires fewer comparisons than linear search, especially for large lists, but the list must be sorted for binary search to work.

Uploaded by

Tanvi Chiman
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/ 4

Chapter 2 - Searching

Searching is a process of finding a particular element in a given list of elements.


The two types of searching are:
(1) Linear search
(2) Binary search

Linear Search
It is a simplest method to find an element in the list. This method is also called as sequential search. In this
search, search element is compared with each element from the list starting from first element. Search process
continues in sequence till list element matches with search element or search process reaches to the comparison
of last element from the list with search element.

Linear search on sorted array:-On sorted array search takes place till list element matches with search element
or search finds a list element greater than search element.

Example: List of elements is represented using an array.


Input list :10, 20, 30, 40, 50 and Search element : 30

Iteration 1: Compare search element with 0th index position element.


10 20 30 40 50

10 ! = 30
Index = Index + 1

Iteration 2: Compare search element with 1st index position element.


10 20 30 40 50

20 ! = 30
Index = Index + 1

Iteration 3: Compare search element with 2nd index position element.


10 20 30 40 50

30 = 30
Number is found
Advantages:-
• Simple method of searching
• Works on unsorted as well as sorted list.
• Easy implementation

Disadvantages:-

• It takes more time with respect to the number of comparisons if element list is large in size.

Algorithm :
Step 1 : Start.
Step 2 : Declare variables array-a[5], search element-se, index position-i, flag=0
Step 3 : Accept values of an array-a and search element-se, initialize i=0.
Step 4 : Repeat step 5 till i < 5 (last location of an array can be N)
Step 5 : [Compare search element with array elements]
If search element=array element [if (se= =a[i])] then
Set flag=1
Then display message as ‘element found’ and stop search process.
Otherwise
Increment index position (i) by one and go to step 4
step 6: If flag = = 0 then
Display message as ‘element not found’
Step 7 : Stop.

Program :
# include <stdio.h>
# include <conio.h>
void main ()
{
int a[5] = {10, 20, 30, 40, 50},se,i,flag=0;
clrscr( );
printf (“Enter search element”);
scanf (“%d”,&se);
for (i = 0; i <5; i ++)
{
if (a[i] = = se)
{
flag=1;
printf(“Element found”);
break;
}
}
if (flag= =0)
printf (“Number not found”);
getch( );
}

Binary Search
Binary search can be performed only on sorted array. In this method, first calculate mid position in an array
and compare the mid position element with the search element. If a match is found then stop the search process
otherwise divide the input list into two parts. First part contains all the numbers less than mid element and
second part contains all the numbers greater than mid element.

To calculate mid position, perform (lowerindex + upperindex) / 2.

The binary search performs the comparison till the element is found or division of list gives one element to
perform comparison.

Example:
Input list : 0, 1, 2, 3, 10, 11, 15, 20, 46, 72 // If the elements are not in sorted order then first perform sorting.
Search element (se) : 15

Input list is implemented with an array of 10 elements so lower index is 0 and upper index is size-1=9.
Iteration 1:
lower = 0 , upper = 9
mid = (lower + upper) / 2= (0 + 9)/2= 4.5 // In case of decimal point answer, consider only integer number.

mid ! = 15
mid<se; lower = mid + 1=5
Iteration 2:
lower = 5 , upper = 9
mid = (Lower + Upper) / 2= (5 + 9) / 2= 7

mid ! = 15
mid>se; upper = mid -1=6
Iteration 3:
lower = 5 , upper = 6
mid = (Lower + Upper) / 2= (5 + 6) / 2= 5.5

mid ! = 15
mid<se; lower = mid + 1=6
Iteration 4:
Lower = 6 , upper = 6
mid = (Lower + Upper) / 2= (6 + 6) / 2= 6

mid = 15
Number is found

Algorithm :
Step 1 : Start
Step 2 : Declare variables array-a[10], search element-se, lower,upper,mid, flag.
Step 3 : Accept values of an array-a, search element-se and initialize lower=0,upper=9,flag=0.
Step 4 : Repeat step 5 & 6 till lower is less than equal to upper
Step 5 : Calculate mid position with the formula as mid = (lower + upper) / 2.
Step 6 : Perform comparison:
If a [mid] = se i.e if mid position element is equal to search element then
Set flag=1 and display message as ‘Element found’ and stop the search process.
Otherwise perform
If a [mid] > se i.e. if mid position element is greater than se then
Set upper = mid -1 and go to step 4.
otherwise
Set lower = mid +1 and go to step 4.
Step 7 : If flag= 0 then
Display message as ‘Element not found’ .
Step 8 : Stop.

Program :
# include <stdio.h>
# include <conio.h>
void main ( )
{
int a[10]={0,1,2,3,10,11,15,20,46,72},se, mid, lower=0, upper=9,flag = 0;
printf (“Enter number to be searched”);
scanf(“%d”, &se);
while (lower< = upper)
{
mid = (lower + upper) / 2;
if ( a[mid] = = se)
{
flag = 1;
printf(“Number found”);
break;
}
else
{
if (a [mid] > se)
upper = mid –l;
else
lower = mid + 1;
}
}
if (flag = = 0)
print f (“Number is not found”);
getch( );
}

Advantages:-
• Fast execution i.e. it takes less number of comparisons for searching an element from a large list.

Disadvantage:-
• List has to be in sorted order i.e binary search works only on sorted array.

Difference between linear search and binary search:


Sr. No. Linear Search Binary Search
1 Search element is compared with all Search element is compared with only mid
array elements starting with 0th position element till search element matches array
element till search elements matches to element or only one element remains in
array element or till the end of array list. array list for comparison.
2 It works on sorted as well as unsorted It works only on sorted array list.
array list.
3. Number of comparisons are more than Number of comparisons are less than linear
binary search for large dataset. search for large dataset.
4 Speed of searching is slow as search Speed of searching is fast as search element
element is compared with all elements. is compared only with mid element.
5. Example Example

You might also like