UrbanPro
true

Learn C++ Language from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Data Structure: Searching

Dr. Lalit Arora
31/01/2017 0 0

Searching 

Searching means finding an element in an array. There are two type of searching techniques :

  1. Linear Search
  2. Binary Search

 In linear search, to find the element array is traversed and checked each element for the position while binary search needs sorted elements to find the element in the array. Binary search compare less elements then linear search and very fast but limitation is that it needs sorted array.

Let see few example programs given below, which represent the concept of both searching :

 

Example 7.9 Program to search an element in the array using Linear Search.

 

#include <iostream.h>

int main (){ 

int item[10], n=0, num, pos[10];  

cout<<”\n Enter 10 elements for array:”; 

for(int i = 0; i < 10; i++)  {  

cout<<”\n Enter the element:”;  

cin>>item[i];   }   

cout<<”\n All elements of array are:\n”; 

for(int i = 0; i < 10; i++)  {  

cout<< item[i] << “\t”;   }    

cout<<”\n Enter element to be searched :”;  

cin >> num;   

//comparing the elements of array       

for(int i = 0; i < 10; i++)   {     

if(num == item[i])        

 pos[n++] = i;            }  

if (n > 0)   {       

  cout << ”\n ” << num << “ is found in array ” << n << “times.”;       

   cout << “\n The positions are :”;       

   for( int i = 0; i < n; i++)              

   cout << pos[i]+1 << “,\t”;   

else       

cout << “\n ” << num << “ is not found in array.” ;

return 0;

}

OUTPUT:

    Enter 10 elements for array:  Enter the element-10  Enter the element-30  Enter the element-45  Enter the element-23  Enter the element-49  Enter the element-10  Enter the element-29  Enter the element-67  Enter the element-34  Enter the element-98   All the elements of array are  10, 30, 45, 23, 49, 10, 29, 67, 34, 98

 

     Enter the element to be searched: 10

 

  10 is found in array 2 times.

  The positions are: 1, 6

 

Example 7.9 Program to search an element in the array using Binary Search.

 

#include <iostream.h>

int main (){ 

int item[10], num, pos, beg, end, mid;  

cout<<”\n Enter 10 elements for array:”; 

for(int i = 0; i < 10; i++)  {  

cout<<”\n Enter the element:”;   cin>>item[i];   }   

cout<<”\n All elements of array are:\n”; 

for(int i = 0; i < 10; i++)  {   cout<< item[i] << “\t”;   }    

cout<<”\n Enter element to be searched :”;  

cin >> num;   

//comparing the elements of array        

beg = 0, end = n-1;   

while(beg <= end)

        {

               mid =( beg + end )/2;

                        if(num == item[mid])

               {

                       n = 1;

                       pos = mid;

                       break;

               }

               else if( num < a[mid])

                       end = mid - 1;

               else

                       beg = mid + 1;

        }  

if (n == 1)

        cout << ”\n” << num<<“is found in array at” << pos=1<< “position.”;

  else

        cout << “\n” << num<< “ is not found in array.”;

 return 0;

OUTPUT:

    Enter 10 elements for array in ascending order:  Enter the element-10  Enter the element-14  Enter the element-15  Enter the element-23  Enter the element-29  Enter the element-34  Enter the element-39  Enter the element-57  Enter the element-64  Enter the element-68   All the elements of array are  10, 14, 15, 23, 29, 34, 39, 57, 64, 68

 Enter the element to be searched: 34

 34 is found in array at 6 position.

 

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

Advantages of C++ Language
Advantages of C++ - C++ is a profoundly convenient dialect and is frequently the dialect of decision for multi-gadget, multi-stage application advancement. - C++ is a protest situated programming dialect...

Why do pointers have a datatype?
Before we start with pointers you must know what is a variable and a datatype. int a; This is the basic line in every program in 'C' . It means that we are asking the compiler to give us 2 bytes of space...

Copy-on-Write
Note: You can read the original write-up at http://som-itsolutions.blogspot.in/2017/01/copy-on-write.html As i was trying to recapitulate several important concepts of OOAD, naturally COW or Copy-On-Write...

C++ Program Sample Application
//Standard Library Functions(Headers Files Used in C++) #include<iostream.h> #include<conio.h> //Main function int main() { //function for clearing the screen clrscr(); //function for...
S

Do (pre & post)Increment and decrement operators behave differently when printed in the same and different line in C++?
Before I explain anything,I would like you to run the following program. void main() {int j =10; cout<<“j before increment =“<<j; cout<<“++j =“<< ++j; cout<<“...

Looking for C++ Language Classes?

Learn from Best Tutors on UrbanPro.

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for C++ Language Classes?

The best tutors for C++ Language Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C++ Language with the Best Tutors

The best Tutors for C++ Language Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more