You are on page 1of 3

#include <iostream>

#include <algorithm>
#include <chrono> //library used for clock timing
#include <ctime>
#include <cstdlib>
#include <iomanip>

using namespace std;


#define MAX 20000 // range for generation of random array

// Linear Search
int LinearSearch(int arr[], int n, int x)
{
for (int i = 0; i < n; i++)
{
if (arr[i] == x)
{
return i;
}
}
return -1;
}

// Recursive Binary Search


int BinarySearchR(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
if (arr[mid] == x)
return mid;
if (arr[mid] > x)
return BinarySearchR(arr, l, mid - 1, x);
if (arr[mid] < x)
return BinarySearchR(arr, mid + 1, r, x);
}
return -1;
}

// Iterative Binary Search


int BinarySearchI(int arr[], int l, int r, int x)
{
while (r >= l)
{
int mid = l + (r - l) / 2;

if (arr[mid] == x)
return mid;
else if (arr[mid] > x)
r = mid - 1;
else
l = mid + 1;
}
return -1;
}

int main()
{
srand(time(NULL));
int n, arr[MAX];
cout << "Enter the number of elements:";
cin >> n;
for (int j = 0; j < n; j++)
{
arr[j] = rand() % MAX; // Gerneration of Random array numbers
}
for (int j = 0; j < n; j++)
{
cout << arr[j] << "\t"; // Printing the array
}

int x;
cout << "\nEnter the element to search: ";
cin >> x;

// Linear Search
auto startL = chrono::high_resolution_clock::now(); // Starting the clock
int resultL = LinearSearch(arr, n, x);
auto endL = chrono::high_resolution_clock::now(); // Stoping the clock

cout << "\n\n## Linear Search ##";


if (resultL == -1)
cout << "\nElement not found!";
else
cout << "\nElement found at the index " << resultL + 1;

auto durationL = chrono::duration_cast<chrono::microseconds>(endL -


startL);

cout << "\nTime taken by Recursive Binary Search: " << fixed <<
durationL.count() << " microseconds" << endl;

// Sorting Array for Binary Searches


sort(arr, arr + n);

// Iterative Binary Search


auto startI = chrono::high_resolution_clock::now();
int resultI = BinarySearchI(arr, 0, n - 1, x);
auto endI = chrono::high_resolution_clock::now();

cout << "\n\n## Iterative Binary Search ##";


if (resultI == -1)
cout << "\nElement not found!";
else
cout << "\nElement found at the index " << resultI + 1;
auto durationI = chrono::duration_cast<chrono::microseconds>(endI - startI);
cout << "\nTime taken by Iterative Binary Search: " << durationI.count() << "
microseconds" << endl;

// Recursive Binary Search


auto startR = chrono::high_resolution_clock::now();
int resultR = BinarySearchR(arr, 0, n - 1, x);
auto endR = chrono::high_resolution_clock::now();
cout << "\n\n## Recursive Binary Search ##";
if (resultR == -1)
cout << "\nElement not found!";
else
cout << "\nElement found at the index " << resultR + 1;
auto durationRecursiveBinary = chrono::duration_cast<chrono::microseconds>(endR
- startR);
cout << fixed << setprecision(3); // Set precision to 3 decimal places
cout << "\nTime taken by Recursive Binary Search: " <<
durationRecursiveBinary.count() << " microseconds" << endl;
return 0;
}

You might also like