[go: up one dir, main page]

0% found this document useful (0 votes)
6 views1 page

PPPPPPPP

The document is a C++ program that implements a search class for performing linear and binary searches on an array. It includes methods for inputting array elements and the search target, as well as calculating the number of comparisons made during the search. The user can choose between linear and binary search, with a note that the array must be sorted for binary search.

Uploaded by

Pulkit Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

PPPPPPPP

The document is a C++ program that implements a search class for performing linear and binary searches on an array. It includes methods for inputting array elements and the search target, as well as calculating the number of comparisons made during the search. The user can choose between linear and binary search, with a note that the array must be sorted for binary search.

Uploaded by

Pulkit Garg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

#include using namespace std; class Search { private: int* A; int X; int n; public: // Constructor

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; }

You might also like