Searching algorithms (Basic)
Searching algorithms (Basic)
1 2024510066
Aim: To implement and analyze searching algorithms, Linear Search and Binary Search
Objectives:
1. To implement the Linear Search algorithm.
2. To implement the Binary Search algorithm.
3. To compare the time complexity of both algorithms.
4. To understand when each algorithm is best suited for use.
Tools Used: Online C++ Compiler - Programiz
Concept:
Linear Search Algorithm:
1. Start
2. Input the array and the target value to search
3. Loop through each element in the array:
● If the current element is equal to the target:
○ Specify the index of the current element and exit the program
● If the loop completes and the target value is not found:
○ Notify that the target value is not found in the array
4. End
Problem Statement:
Implement Linear and Binary searching techniques in c++.
Solution:
Linear search:
#include <iostream>
using namespace std;
class LinearSearch
{
public:
int *getInput(int size)
{
int *arr = new int[size];
for (int i = 0; i < size; i++)
{
cout << "Enter value of element at position " << i << ": ";
cin >> arr[i];
}
return arr;
}
int main()
{
Prabhat Anand Tiwari Practical no. 1 2024510066
LinearSearch ls;
int n = 0;
cout << "Enter size of the array: ";
cin >> n;
int *arr = ls.getInput(n);
int target = 0;
cout << "Enter the element to search: ";
cin >> target;
ls.linearSearch(arr, n, target);
return 0;
}
Output:
Prabhat Anand Tiwari Practical no. 1 2024510066
Binary search:
#include <iostream>
using namespace std;
class BinarySearch
{
public:
int *getInput(int size)
{
int *arr = new int[size];
for (int i = 0; i < size; i++)
{
cout << "Enter value of element at position " << i << ": ";
cin >> arr[i];
}
return arr;
}
{
int mid = (leftPtr + rightPtr) / 2;
if (arr[mid] == target)
{
cout << "Element " << target << " is found at position " << mid << " of
sorted array" << endl;
return;
}
else if (arr[mid] > target)
{
rightPtr = mid - 1;
}
else
{
leftPtr = mid + 1;
}
}
cout << "Element not found";
}
};
int main()
{
BinarySearch bs;
int n = 0;
cout << "Enter size of the array: ";
cin >> n;
int *arr = bs.getInput(n);
int target = 0;
cout << "Enter the element to search: ";
cin >> target;
bs.bubbleSort(arr, n);
bs.binarySearch(arr, n, target);
return 0;
}
Output:
Prabhat Anand Tiwari Practical no. 1 2024510066
Observation:
Linear Search is easy to apply, but is slower. Since it searches through each and every
element in the array, this makes the time complexity proportional to the size of array
which is O(n).
Binary Search works much faster since it keeps reducing the scope of elements in the
array to be searched for finding the target value. But it can only be used on sorted data.
If used on unsorted data, time complexity of the sorting algorithm used to sort the array
should be atleast O (log n) to keep the time complexity low.
Binary Search requires fewer comparisons to find the target value hence, is more
efficient provided the array is sorted.
Linear search can give results quickly if the element is in the start of comparisons which
cannot be assumed to be happening often hence should be avoided with larger dataset.