Saturday, June 13, 2015

Menu Driven C++ program to implement Linear search and Binary search

/*-------------------------------------------------------------------------------------------------------------
File Name:      17 linearandbinary.cpp
Author:           sujith
Reg No:           141740
Program:        Write Menu Driven C++ program to implement the following
                                                              i.      Linear search
                                                            ii.      Binary search
Date:               23-04-2015
--------------------------------------------------------------------------------------------------------------*/
#include<iostream>
#include<stdio.h>
using namespace std;
int linearsearch(int a[20],int key,int n);
int binarysearch(int a[20],int key,int n);
int main()
{
        int a[20],k,n,ch,ans,i;
        cout<<"Enter the total number of elements\n";
        cin>>n;
        while(1){
                cout<<"\nMenu\n";
                cout<<"1.linear search\n";
                cout<<"2.binary search\n";
                cout<<"3.exit\n";
                cout<<"Enter your choice\n";
                cin>>ch;
                switch(ch){
                        case 1:
                                cout<<"Enter the elements\n";
                                for(i=0;i<n;i++){
                                        cin>>a[i];
                                }
                                cout<<"Enter the key elememts\n";
                                cin>>k;
                                ans=linearsearch(a,k,n);
                                if(ans!=-1)
                                        cout<<"element\t"<<k<<"\tis found\n";
                                else
                                        cout<<"Element\t"<<k<<"\tis not found\n";
                                break;
                        case 2:
                                cout<<"Enter the elements\n";
                                for(i=0;i<n;i++){
                                        cin>>a[i];
                                }
                                cout<<"Enter the key elememts\n";
                                cin>>k;
                                ans=binarysearch(a,k,n);
                                if(ans!=-1)
                                        cout<<"element\t"<<k<<"\tis found\n";
                                else
                                        cout<<"Element\t"<<k<<"\tnot found\n";
                                break;
                        case 3:
                                exit(0);
                        default:
                                cout<<"Invalid entry\n";
                }
        }
}
int linearsearch(int a[20],int key,int n)
{
        int i;
        for(i=0;i<n;i++){
                if(key==a[i])
                        return i;
        }

        return -1;
}
int binarysearch(int a[20],int key,int n)
{
        int high,low,mid;
        low=0;
        high=n-1;
        while(low<=high){
                mid=(low+high)/2;
                if(a[mid]==key)
                        return mid;
                else if(a[mid]<key)
                        low=mid+1;
                else
                        high=mid-1;
        }
        return -1;
}



















OUTPUT:

[141740@localhost ~]$ g++ linearandbinary.cpp
[141740@localhost ~]$ ./a.out
Enter the total number of elements
5

Menu
1.linear search
2.binary search
Enter your choice
1
Enter the elements
1          5          6          7          4
Enter the key elememts
7
element 7   is found

Menu
1.linear search
2.binary search
3.exit
Enter your choice
1
Enter the elements
55        66        77        88        99
Enter the key elememts
100
Element 100  is not found

Menu
1.linear search
2.binary search
3.exit
Enter your choice
2
Enter the elements
15        55        66        44        22
Enter the key elememts
55
element 55      is found

Menu
1.linear search
2.binary search
3.exit
Enter your choice
2
Enter the elements
55        66        45        56        23
Enter the key elememts
100
Element 100  not found

Menu
1.linear search
2.binary search
3.exit
Enter your choice
3

[141740@localhost ~]$

1 comment: