Users Online

· Guests Online: 94

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C++ Program to Implement Linear Search using Recursion

C++ Program to Implement Linear Search using Recursion

 

 

This is a C++ Program to implement Linear Search Algorithm using recursion.

Problem Description

We have to create a C++ Program which finds the position of an element in an array using Linear Search Algorithm using recursion.

Expected Input and Output

1. Average Case: When the element searched for is any random element in the array.

If the input array is {4, 6, 1, 2, 5, 3},
and the user has searched for 2
then the output will be Position: = 4

2. Best Case: When the element searched for is the first element of array.
For example:

If the input array is arr = {66, -3, 31},
and user has searched for 66,
then the output will be Position: = 1

3. Worst Case: When the element searched for is the last element of the array.
For example:

If the input array is {1, 3, 6, 1, 9}
and the user has searched for 9,
then the output will be Position: = 5
Problem Solution

1. We first have to create an array of numbers by taking input from user. We have to input an array of numbers and then apply the linear search algorithm to find the position of an element in an array, if it exists.
2. In order to look for an element in an array, we’ll go sequentially in increasing index values. If we encounter the element requested by the user we will return the position of that element in array, but if it is not there we will return -1 which indicates the absence of element which was searched.

Program/Source Code

Here is source code of the C++ Program to implement Linear Search Algorithm using Recursion. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on Windows 10. The program output is also shown below.

  1. /* C++ Program to implement Linear Search Algorithm recursively */
  2. #include <iostream>
  3. using namespace std;
  4. class LS
  5. {
  6. public:
  7.     int RecursiveLS(int arr[], int value, int index, int n)
  8.     {
  9.         int pos = 0;
  10.         if(index >= n)
  11.         {
  12.             return 0;
  13.         }
  14.         if(arr[n-1] == value)
  15.         {
  16.             pos = n;
  17.             return pos;
  18.         }
  19.         else if (arr[index] == value)
  20.         {
  21.             pos = index + 1;
  22.             return pos;
  23.         }
  24.         else
  25.         {
  26.             return RecursiveLS(arr, value, index+1, n-1);
  27.         }
  28.         return pos;
  29.     }
  30. };
  31. int main()
  32. {
  33.     LS l1;
  34.     int n, value, pos, m = 0, arr[100];
  35.     cout<<"Enter the total elements in the array  ";
  36.     cin>>n;
  37.     cout<<"Enter the array elements  ";
  38.     cout<<endl;
  39.     for (int i = 0; i < n; i++)
  40.     {
  41.         cin>>arr[i];
  42.     }
  43.     cout<<"Enter the element to search  ";
  44.     cin>>value;
  45.     pos =  l1.RecursiveLS(arr, value, 0, n);
  46.     if (pos != 0)
  47.     {
  48.         cout<<"Element found at pos  "<<pos;
  49.     }
  50.     else
  51.     {
  52.         cout<<"Element not found";
  53.     }
  54.     return 0;
  55. }
Program Explanation

1. In Linear search, we search an element or value in a given array by traversing the array from the starting, till the desired element or value is found. The array is searched sequentially and the position is returned if the key element to be searched is available in the array, otherwise “Element not found” is printed.
2. Here in this C++ Program we have created a recursive function called RecursiveLS(), which takes in 4 input parameters and returns the position of element in a array which is searched by the user.
3. If the element that is searched is the first or the last element of the array, we directly return the index, but if it is not either of them we decrease the size of array by 2, by eliminating the first and last elements of the array, which means when the RecursiveLS() is called second time the array size will be (n-2).
4. This will go on till the element is found.

Runtime Test Cases
1. Enter the total elements in the array  6
   Enter the array elements
   4
   6
   1
   2
   5
   3
   Enter the element to search  2
   Element found at pos  4
 
2. Enter the total elements in the array  3
   Enter the array elements
   66
   -3
   31
   Enter the element to search  66
   Element found at pos  1
 
3. Enter the total elements in the array  5
   Enter the array elements
   1
   3
   6
   1
   9
   Enter the element to search  9
   Element found at pos  5

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.70 seconds
10,835,011 unique visits