PPPPPPPP
PPPPPPPP
Search(int size) { n = size; A = new int[n]; } ~Search() { delete[] A; } // Function to take input from
the user for array elements void inputArray() { cout << "Enter " << n << " elements of the
array:\n"; for (int i = 0; i < n; i++) { cin >> A[i]; } } // Function to input the element to search for
void inputElement() { cout << "Enter the element to search for: "; cin >> X; } // Linear Search
Function int linearSearch() { int comparisons = 0; for (int i = 0; i < n; i++) { comparisons++; if (A[i]
== X) { cout << "Linear Search: " << comparisons << " comparisons made.\n"; return i; //
Element found, return its index } } cout << "Linear Search: " << comparisons << " comparisons
made.\n"; return -1; // Element not found } // Binary Search Function int binarySearch() { int
comparisons = 0; int left = 0, right = n - 1; while (left <= right) { comparisons++; int mid = left +
(right - left) / 2; if (A[mid] == X) { cout << "Binary Search: " << comparisons << " comparisons
made.\n"; return mid; // Element found, return its index } else if (A[mid] < X) { left = mid + 1; }
else { right = mid - 1; } } cout << "Binary Search: " << comparisons << " comparisons made.\n";
return -1; // Element not found } }; int main() { int size; cout << "Enter the size of the array: "; cin
>> size; // Create an instance of the Search class Search search(size); // Take input for array
and element to search search.inputArray(); search.inputElement(); // Ask the user for the type of
search int choice; cout << "Choose the search method:\n"; cout << "1. Linear Search\n"; cout <<
"2. Binary Search (Array must be sorted)\n"; cout << "Enter choice (1 or 2): "; cin >> choice; int
result; if (choice == 1) { result = search.linearSearch(); } else if (choice == 2) { // Sort for Binary
Search sort(search.A, search.A + size); cout << "Sorted Array: "; for (int i = 0; i < size; i++) { cout
<< search.A[i] << " "; } cout << endl; result = search.binarySearch(); } else { cout << "Invalid !"
<< endl; return 0; } // Output the result if (result == -1) { cout << "Element not found in array.\n"; }
else { cout << "Element found at index " << result << ".\n"; } return 0; }