You are on page 1of 4

210170107518 Analysis and Design of algorithm Batch:H4

Practical No.: 3
 Aim: Implementation and Time analysis of linear and binary search algorithm using
divide-and-conquer approach.

1) Linear Search:

Linear search is a sequential searching algorithm where we start from one end and
check every element of the list until the desired element is found. It is the simplest
searching algorithm.

 Algorithm and Analysis:

STEP-1: SET I TO 1
STEP-2: IF I > N THEN GO TO STEP 7
STEP 3: IF A[I] = X THEN GO TO STEP 6
STEP 4: SET I TO I + 1
STEP 5: GO TO STEP 2
STEP 6: PRINT ELEMENT X FOUND AT INDEX I AND GO TO STEP 8
STEP 7: PRINT ELEMENT NOT FOUND
STEP 8: EXIT

 Code:

#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;

bool linearSearch(int arr[], int n, int value){


for(int i=0; i<n;i++){
if(arr[i] == value) return true;
}
return false;

int main() {
cout <<"Enter the size of an array: ";
int n;
cin >> n;
int *arr = new int(n);
210170107518 Analysis and Design of algorithm Batch:H4

cout <<"Enter the elements of array:"<<endl;


for (int i = 0; i < n; i++){
cin >> arr[i];
}
int value;
cout<<"\nEnter the value to be Searched: ";
cin >> value;

auto start = high_resolution_clock::now();

if(linearSearch(arr,n,value)){
cout<<"Value Found!\n";
}else{
cout<<"Value Not Found!\n";
}

auto elapsed = high_resolution_clock::now() - start;

long long ms = duration_cast<microseconds>(


elapsed).count();

double sec = (double)(ms)/1000000;


cout<<"\nTime Taken to search value with array of '"<<n<<"' elements in
[LINEAR SEARCH]: \n";
cout<<"Microseconds:: "<<ms<<endl;
cout<<"Seconds:: "<<sec<<endl;
return 0;
}

 Output:
210170107518 Analysis and Design of algorithm Batch:H4

2) Binary Search:

A binary search is an advanced type of search algorithm that finds and fetches data
from a sorted list of items. Its core working principle involves dividing the data in the
list to half until the required value is located and displayed to the user in the search
result. Binary search is commonly known as a half-interval search or a logarithmic
search.

 Algorithm and Analysis:

STEP 1: SET BEG = LOWER_BOUND, POS = - 1, END = UPPER_BOUND


STEP 2: REPEAT STEPS 3 AND 4 WHILE BEG <=END
STEP 3: SET MID = (BEG + END)/2
STEP 4: IF A[MID] = VAL, SET POS = MID
GO TO STEP 6
ELSE IF A[MID] > VAL
END = MID - 1
ELSE
SET BEG = MID + 1
STEP 5: IF POS = -1
PRINT "VALUE IS NOT PRESENT IN THE ARRAY"
STEP 6: EXIT

 Code:

#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;

int binarySearch(int array[], int x, int low, int high) {


while (low <= high) {
int mid = low + (high - low) / 2;
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
210170107518 Analysis and Design of algorithm Batch:H4

int main() {
int array[] = {2, 4, 0, 1, 9};
int x = 99999;
int n = sizeof(array) / sizeof(array[0]);

auto start = high_resolution_clock::now();


int result = binarySearch(array, x, 0, n-1);

cout<<"\nTOTAL ELEMENTS -- " <<n;


cout<<"\nElement to search: "<<x;
(result == -1) ? cout << "\nElement not found" : cout << "\nElement found at
index: " << result+1;
auto elapsed = high_resolution_clock::now() - start;
long long ms = duration_cast<microseconds>(
elapsed).count();

double sec = (double)(ms)/1000000;


cout<<"\nTime Complexity of searching elements in [BINARY SEARCH]\n";
cout<<"Microseconds- "<<ms<<endl;
cout<<"Seconds- "<<sec<<endl;
}

 Output:

You might also like