Write a menu driven program to implement linear and binary search also find the location of its first occurrence


#include
#include
#include
#include

int i, k, m, n, item;

class search
{
            int a[' '];
            public:
                        int sort(int a[], int n);
                        int bsearch(int a[], int n, int item);
                        int lsearch(int a[], int n, int item);
};

int search::sort(int a[], int n)
{
            int i, j, temp;
            for (i=0 ;i
            {
                        for (j=0 ;j
                        {
                                    if (a[j]>a[j+1])
                                    {
                                                temp = a[j];
                                                a[j] = a[j+1];
                                                a[j+1] = temp;
                                    }
                        }
            }
            return a[i];
}

int search::bsearch(int a[], int n, int item)
{
            int low, high, mid;
            low = 0;
            high = n;
            while(low<=high)
            {
                        mid = (low + high) / 2;
                        if (item
                                    high = mid - 1;
                        else if (item > a[mid])
                                    low = mid +1;
                        else
                                    return mid + 1;
            }
            return -1;
}

int search::lsearch(int a[], int n, int item)
{
            int i, pos;
            i = 0;
            pos = 0;
            while (i
            {
                        if (a[i]==item)
                        {
                                    pos = 1;
                                    i++;
                                    break;
                        }
                        else
                                    i++;
            }
            if (pos==1)
                        cout << "\nItem found at position : " << i;
            else
                        cout << "\nItem not found ";
            return 0;
}

void main()
{
            int a[' '], n;
            n = 0;
            search obj;
            int ch, k;
            char c = 'y';
            clrscr();

            cout << "Program to demonstrate Linear Search and Binary Search \n" ;
            while (c!='n')
            {
                        cout << "\nEnter number of elements : " ;
                        cin >> n;
                        if (n<=0)
                        {
                                    cout << "\nEnter the appropriate number of elements" ;
                        }
                        else
                        {
                                    cout << "Enter the elements : \n" ;
                                    for (m=0 ;m
                                    {
                                                cout << "a[ " << m << " ] = " ;
                                                cin >> a[m];
                                    }
                                    cout << "\nEnter the element to search : ";
                                    cin >> item;
                                    cout << "\n\nChoose any given option : ";
                                    cout << "\n\t 1. Linear Search" ;
                                    cout << "\n\t 2. Binary Search" ;
                                    cout << "\n\t 3. Exit";
                                    cout << "\n\nEnter your choice : " ;
                                    cin >> ch;
                                    switch (ch)
                                    {
                                    case 1: cout << "\nPerform Linear Search :\n" ;
                                                                          obj.lsearch(a, n, item);
                                                                          break;
                                    case 2: cout << "\nPerform Binary Search : \n" ;
                                                                          obj.sort(a,n);
                                                                          cout << "Elements after sorted : \n" ;
                                                                          for (int l=0 ;l
                                                                          {
                                                                                      printf("a[ %d ] = %d \t", l, a[l]);
                                                                          }
                                                                          int p = obj.bsearch(a, n, item);
                                                                          cout << "\nItem found at position : " << p;
                                                                          break;
                                    case 3: exit(0);
                                    default: cout << "\nInvalid choice" ;
                                    }
                        }
                        cout << "\nDo you want to continue? (y/n) : " ;
                        cin >> c;
            }
            getch();
}

Comments

Popular posts from this blog

To convert hexadecimal to decimal numbers.

To convert hexadecimal to decimal numbers.