#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;
// Linear Search function
int linearSearch(int arr[], int size, int target) {
for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i; // Return index if found
}
}
return -1; // Return -1 if not found
}
int main() {
int size;
cout << "Enter the size of the array: ";
cin >> size;
int* arr = new int[size];
cout << "Enter " << size << " elements: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
int target;
cout << "Enter the element to search: ";
cin >> target;
// Measure execution time
auto start = high_resolution_clock::now();
int result = linearSearch(arr, size, target);
auto end = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(end - start);
// Output result
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found in the array" << endl;
}
cout << "Execution time: " << [Link]() << " nanoseconds" << endl;
// Clean up dynamic memory
delete[] arr;
return 0;
}