0% found this document useful (0 votes)
3 views

Searching algorithms (Basic)

Uploaded by

prabhumiui328
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Searching algorithms (Basic)

Uploaded by

prabhumiui328
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Prabhat Anand Tiwari Practical no.

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

Binary Search Algorithm:


1. Start
2. Input the array and the target value to search
3. Sort the array
4. Set two pointers:
● Left pointer to the first index
● Right pointer to the last index
5. While the left pointer is less than or equal to the right pointer:
● Calculate the middle index
● If the element at the middle index is equal to the target:
○ Specify the middle index and exit the program
● If the element at the middle index is less than the target:
○ Move the left pointer one index right of the middle
● If the element at the middle index is greater than the target:
○ Move the right pointer one index left of the middle
6. If the loop completes (left pointer is greater than right pointer) and the target is
not found:
● Notify that the target value is not found in the array
7. End
Prabhat Anand Tiwari Practical no. 1 2024510066

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;
}

void linearSearch(int arr[], int size, int target)


{
for (int i = 0; i < size; i++)
{
if (arr[i] == target)
{
cout << "Element " << target << " is found at position " << i << endl;
return;
}
}
cout << "Element not found";
}
};

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;
}

void bubbleSort(int arr[], int size)


{
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
arr[j] += arr[j + 1];
arr[j + 1] = arr[j] - arr[j + 1];
arr[j] -= arr[j + 1];
}
}
}
}

void binarySearch(int arr[], int size, int target)


{
int leftPtr = 0;
int rightPtr = size - 1;
while (leftPtr <= rightPtr)
Prabhat Anand Tiwari Practical no. 1 2024510066

{
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.

You might also like