google search

Popular Posts


Welcome to BCA Notes

>>>>>>>>>>>>>>>>

Visitors

Search This Blog

Blogger templates

Visitor Map


Thursday 21 August 2014

Linear Search algorithm

#include <iostream.h>
#include<conio.h>
void main()
{
    clrscr();
    int arr[20];    //Array to be searched
    int n;      //Number of elements in the array
    int i;

    // Get the number of elements to store in the array
    while (1)
    {
cout << "Enter the number of elements in the array(1-20): ";
cin >> n;
if ((n>0) && (n <= 20))
   break;
else
   cout << "\nArray should have minimum 1 and maximum 20 elements.\n\n";
    }

    //Accept array elements
    cout << "\n-----------------------\n";
    cout << " Enter array elements\n";
    cout << "-----------------------\n";

    for (i = 0; i < n; i++)
    {
cout << "<" << (i + 1) << "> ";
cin >> arr[i];
    }

    char ch;
    int ctr;

    do
    {
//Accept the number to be searched
int item;
cout << "\nEnter the element you want to search: ";
cin >> item;

//Apply linear search
ctr = 0;
int find=0;
for (i = 0; i < n; i++)
{
   ctr++;

   if (arr[i] == item)
   {
cout << "\n" << item << " found at position " << i + 1 << endl;
find++;
//break;
   }

}


if ((i == n) && (find==0))
   cout << endl << item << " not found in the array\n";

cout<<"\nNumber of comparisons: "<<ctr;

cout << "\n\nContinue search (y/n):";
cin >> ch;

    } while ((ch == 'y') || (ch == 'Y'));
}





0 comments:

Post a Comment