Unit II Searching
Unit II Searching
Topic: Searching
Prepared By: Ms. Sonu Sharma, Assistant Professor
Department of CSA
CONTENTS
1. Introduction
2. What is Searching?
3. Searching Terminologies?
6. Examples.
8. Conclusion
9. References
Objectives
Objectives
Course Outcomes
There are two popular search methods that are widely used in order
to search some item into the list. However, choice of the algorithm
Searching depends upon the arrangement of the list.
• Linear Search
• Binary Search
Searching terminologies:
Target Element:
There is always a specific target element or item that you want to find
within the data collection. This target could be a value, a record, a key, or
any other data entity of interest.
Search Space:
The search space refers to the entire collection of data within which you
are looking for the target element.
Efficiency: Efficient searching algorithms improve program
performance.
Data Retrieval: Quickly find and retrieve specific data from
large datasets.
Database Systems: Enables fast querying of databases.
Binary Search
LINEAR SEARCH
Linear search is the simplest search algorithm and often called sequential search.
In this type of searching, we simply traverse the list completely and match each element of the
list with the item whose location is to be found.
If the match is found then the location of the item is returned otherwise the algorithm returns
NULL.
Linear search is mostly used to search an unordered list and small-sized list in which the items
are not sorted.
EXAMPLE
Consider the following list of elements and the element to be searched...
ALGORITHM OF LINEAR SEARCH
LINEAR_SEARCH(A, N, VAL)
Step 1: [INITIALIZE] SET POS = -1
Step 2: [INITIALIZE] SET I = 1
Step 3: Repeat Step 4 while I<=N
Step 4: IF A[I] = VAL
SET POS = I
PRINT POS
Go to Step 6
[END OF IF]
SET I = I + 1
[ENDOF LOOP]
Step 5: IF POS = -1
PRINT " VALUE IS NOT PRESENTIN THE ARRAY "
[END OF IF]
Step 6: EXIT
LINEAR SEARCH BY ITERATIVE METHOD
#include <stdio.h>
int main()
{
int arr[100], num, i, n, found = 0, pos = -1;
printf("\n Enter the number of elements in the array : ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
printf("\n Enter the number that has to be searched : ");
scanf("%d", &num);
for(i=0;i<n;i++)
{
if(arr[i] == num)
{
found =1;
pos=i;
printf("\n %d is found in the array at position= %d", num,i+1);
break;
}
}
if (found == 0)
printf("\n %d does not exist in the array", num);
return 0;
}
OUTPUT
Enter the number of elements in the array: 5
Enter the elements: 1
2
5
6
7
Enter the number that has to be searched: 6
6 is found in the array at position= 4
LINEAR SEARCH BY RECURSIVE METHOD
#include <stdio.h>
int recursiveLinearSearch(int arr[], int size, int val, int index);
int main()
{
int arr[20], num, n, result;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
printf("Enter the elements: ");
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
printf("Enter the number that has to be searched: ");
scanf("%d", &num);
result = recursiveLinearSearch(arr, n, num, 0);
if (result == -1)
{
printf("%d does not exist in the array\n", num);
}
else
{
printf("%d is found in the array at position %d\n", num, result + 1);
}
return 0;
}
int recursiveLinearSearch(int arr[], int size, int val, int index) {
// Base case: If index reaches the size of the array, the element is not found
if (index == size)
{
return -1;
}
// If the element is found at the current index, return the index
if (arr[index] == val)
{
return index;
}
return recursiveLinearSearch(arr, size, val, index + 1);
}
OUTPUT
Binary search follows a divide and conquer approach in which, the list is divided into two
halves, and the item is compared with the middle element of the list. If the match is found then,
the location of the middle element is returned otherwise, we search into either of the halves
depending upon the result produced through the match.
Binary Search Implementation
Step 1 - Read the search element from the user.
Step 7 - If the search element is larger than middle element, 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 sublist contains only one element.
Step 9 - If that element also doesn't match with the search element, then
display "Element is not found in the list!!!" and terminate the function.
BINARY SEARCH ALGORITHM
0 1 2 3 4 5 6 7 8
1 5 7 8 13 19 20 23 29
We have to use the below formula to calculate the mid of the array
mid = (beg + end)/2
So, in the given array -
beg = 0
end = 8
mid = (0 + 8)/2 = 4. So, 4 is the mid of the array.
However, if VAL is not equal to A[MID], then the values of BEG, END, and MID will be changed
depending on whether VAL is smaller or greater than A[MID].
(a) If VAL < A[MID], then VAL will be present in the left segment of the array. So, the value of
END will be changed as END = MID – 1.
(b) If VAL > A[MID], then VAL will be present in the right segment of the array. So, the value of
BEG will be changed as BEG = MID + 1.
In 1st step :
1. BEG = 0
2. END = 8
3. MID = 4
4. a[mid] = a[4] = 13 < 23, therefore
in Second step:
1. Beg = mid +1 = 5
2. End = 8
3. mid = 13/2 = 6
4. a[mid] = a[6] = 20 < 23, therefore;
in third step:
1. beg = mid + 1 = 7
2. End = 8
3. mid = 15/2 = 7
4. a[mid] = a[7]
5. a[7] = 23 = item;
6. therefore, set location = mid;
7. The location of the item will be 7.
BINARY SEARCH BY ITERATIVE METHOD
#include <stdio.h>
int smallest(int arr[100], int k, int n); // Added to sort array
void selection_sort(int arr[100], int n); // Added to sort array
int main()
{
int arr[100], num, i, n, beg, end, mid, found=0;
printf("\n Enter the number of elements in the array: ");
scanf("%d", &n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d", &arr[i]);
}
selection_sort(arr, n); // Added to sort the array
printf("\n The sorted array is: \n");
for(i=0;i<n;i++)
printf(" %d\t", arr[i]);
printf("\n\n Enter the number that has to be searched: ");
scanf("%d", &num);
beg = 0, end = n-1;
while(beg<=end)
{
mid = (beg + end)/2;
if (arr[mid] == num)
{
printf("\n %d is present in the array at position %d", num, mid+1);
found =1;
break;
}
else if (arr[mid]>num)
end = mid-1;
else
beg = mid+1;
}
if (beg > end && found == 0)
printf("\n %d does not exist in the array", num);
return 0;
}
int smallest(int arr[], int k, int n)
{
int pos = k, small=arr[k], i;
for(i=k+1;i<n;i++)
{
if(arr[i]< small)
{
small = arr[i];
pos = i;
}
}
return pos;
}
void selection_sort(int arr[],int n)
{
int k, pos, temp;
for(k=0;k<n;k++)
{
pos = smallest(arr, k, n);
temp = arr[k];
arr[k] = arr[pos];
arr[pos] = temp;
}
}
OUTPUT
Enter the number of elements in the array: 6
Enter the elements: 5
9
6
8
4
3
The sorted array is:
3 4 5 6 8 9
Enter the number that has to be searched: 6
6 is present in the array at position 4
LINEAR SEARCH V/S BINARY SEARCH
Parameters Linear Search Binary Search
Linear Search sequentially checks each element in Binary Search continuously divides the sorted list,
Definition
the list until it finds a match or exhausts the list. comparing the middle element with the target value.
The time complexity is O(n), where n is the number The time complexity is O(log n), making it faster for
Time Complexity
of elements in the list. larger datasets.
Efficiency Less efficient, especially for large datasets. More efficient, especially for large datasets.
Data Requirement Does not require the list to be sorted. Requires the list to be sorted.
Use Case Suitable for small and unsorted datasets. Ideal for large and sorted datasets.
Thank You!!