C++ Program to Implement Linear Search using Templates

Linear Search program using templates is very similar to a non-generic version ( without templates ). If you don’t know what linear search is or how it works then refer to this.

Before anything, you must be aware of why we use templates. With the help of templates, we can use the same linear search function to search values in different types of array.

The code is self-explanatory. If you face any problem, feel free to comment. We will solve it as quickly as possible.

Program to Implement Linear Search using Templates

``````#include <iostream>
using namespace std;

// linear search template function
// arr: array
// n: size of array
// x: value to search
// the function returns the index of x if it is present in arr
// otherwise it returns -1
template <class T>
int LinearSearch(T arr[], int n, T x) {

for (int i = 0; i < n; ++i) {

if (arr[i] == x)
return i;

}

return -1;

}

int main() {

int arr[] = { 6 , 43 ,23 ,6, 12 ,43, 3, 4, 2, 6 };
int n, index, x;
n = sizeof(arr) / sizeof(int); // size of arr

cout << "Integer Array: ";
for (int i = 0; i < n; ++i)	cout << arr[i] << ' ';
cout << endl;

cout << "Enter Value you want to search: ";
cin >> x;

index = LinearSearch(arr, n, x);

if (index != -1)
cout << x << " is present in the array at position " << index << endl;
else
cout << x << " is not present in the array \n" << endl;

/*-----------------------------------------------------------------------------*/

char charArr[] = { 'A', 'v', 'D', 'R', 'T','u', 'j', 'o' };
char c;
n = sizeof(charArr) / sizeof(char);

cout << "Char Array: ";
for (int i = 0; i < n; ++i)	cout << charArr[i] << ' ';
cout << endl;

cout << "Enter character you want to search: ";
cin >> c;

index = LinearSearch(charArr, n, c);

if (index != -1)
cout << c << " is present in the array at position " << index << endl;
else
cout << c << " is not present in the array " << endl;

}``````

Output

Program to Implement Linear Search using Templates ( Recursive )

``````#include <iostream>
using namespace std;

// linear search template function
// arr: array
// n: size of array
// x: value to search
// the function returns the index of x if it is present in arr
// otherwise it returns -1
template <class T>
int LinearSearch(T arr[], int n, T x, int i = 0) {

if (i == n)
return -1;

if (arr[i] == x)
return i;
else
return LinearSearch(arr, n, x, i + 1);

}

int main() {

int arr[] = { 6 , 43 ,23 ,6, 12 ,43, 3, 4, 2, 6 };
int n, index, x;
n = sizeof(arr) / sizeof(int); // size of arr

cout << "Integer Array: ";
for (int i = 0; i < n; ++i)	cout << arr[i] << ' ';
cout << endl;

cout << "Enter Value you want to search: ";
cin >> x;

index = LinearSearch(arr, n, x);

if (index != -1)
cout << x << " is present in the array at position " << index << endl << endl;
else
cout << x << " is not present in the array \n" << endl;

/*-----------------------------------------------------------------------------*/

char charArr[] = { 'A', 'v', 'D', 'R', 'T','u', 'j', 'o' };
char c;
n = sizeof(charArr) / sizeof(char);

cout << "Char Array: ";
for (int i = 0; i < n; ++i)	cout << charArr[i] << ' ';
cout << endl;

cout << "Enter character you want to search: ";
cin >> c;

index = LinearSearch(charArr, n, c);

if (index != -1)
cout << c << " is present in the array at position " << index << endl;
else
cout << c << " is not present in the array " << endl;

}``````

Output