Professional Documents
Culture Documents
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.
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;
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
if(linearSearch(arr,n,value)){
cout<<"Value Found!\n";
}else{
cout<<"Value Not Found!\n";
}
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.
Code:
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;
int main() {
int array[] = {2, 4, 0, 1, 9};
int x = 99999;
int n = sizeof(array) / sizeof(array[0]);
Output: