Friday, October 7, 2016

Linear Search and Binary Search using C and C++ Language - recursive

Write programs in C/C++ language to find a specific number from a user input array elements by following linear search and binary search algorithms.

Method – 1: Linear search

Code

#include<conio.h>
#include<iostream>
using namespace std;

int main()
{
                    //declaring variables and array
    int max = 5;
    int i,j=0,s;
    int a[max];
                    //Taking input(Numbers) from user.
    for(i=0;i<max;i++)
    {
        cout<<"Enter "<<(i+1)<<"th Number: ";
        cin>>a[i];
    }
                    //Asking user to enter the number he/she wants to search
    cout<<"\nEnter number to search: ";
    cin>>s;
                    //Searching the number
    cout<<"\nAfter Searching:";
    for(i=0;i<max;i++)
    {
        if (a[i]==s)
        {
           cout<<"\n\tThe number ("<<a[i]<<") is at "<<(i+1)<<"th position.";
           j = 1;
        }
    }
   
                   //Display Number and its position (after searching)
    if (j == 0)
          cout<<"Entered number is not in the list.";
   
    cout<<"\n\nPress any key to exit.";
    getch();
    return 0;  
}

Output

Linear Search c/c++

Method – 2: Recursive linear search

Code

#include <iostream>
#include <conio.h>
using namespace std;

int linearSearch( const int array[], int length, int value);

int main()
{
    const int arraySize = 100;
    int a[arraySize];
    int element;

    for( int i = 0; i < arraySize; i++)
    {
         a[i] = 2 * i;
    }
   
    element = linearSearch( a, arraySize, 8);

    if( element != -1 )
        cout << "Found value in element " << element << endl;

    else
        cout << "Value not found." << endl;

    getch();
    return 0;
}

int linearSearch( const int array[], int length, int value)
{
    if(length==0)
        return -1;
    else if(array[length-1]==value)
        return length-1;
    else return linearSearch( array, length-1, value);
}

Output

Recursive Linear Search c/c++

Method – 3: Binary Search

Code

#include<conio.h>
#include<iostream>
using namespace std;

int main()
{
                    //declaring variables and array
    int max = 10;
    int i,j,s,m,location,k;
    int a[max];
                    //Taking input(Numbers) from user.
    for(i=0;i<max;i++)
    {
        cout<<"Enter "<<(i+1)<<"th Number: ";
        cin>>a[i];
    }
                    //Asking user to enter the number he/she wants to search
    cout<<"\nEnter number to search: ";
    cin>>s;
                    //Searching the number
    i = 0;
    j = max;
    while (i < j)
    {
          m = ((i+j)/2);
          if (s > a[m])
             i = m+1;
          else
              j = m;
    }
   
    if (s == a[i])
    {             //if searched number is equal to a[i]
       cout<<"\n\nEntered number ("<<s<<") is at "<<(i+1)<<"th position.";
    }
    else
    {             //if the searched number is not in the list
        cout<<"\n\nEntered number is not in the list.";
    }
   
    cout<<"\n\nPress any key to exit.";
    getch();
    return 0;  
}

Output

Binary Search c/c++

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact