m2skills

bsearch cpp

Mar 30th, 2017
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. /* Program to implement binary Search algorithm*/
  2. #include <iostream>
  3.  
  4. using namespace std;
  5. int flag;
  6. class Search
  7. {
  8.     private:
  9.         int element[100],index,counter,start,last,mid;
  10.     public:
  11.         void getdata()
  12.         {
  13.             cout<<"\nHOW MANY ELEMENTS DO YOU WANT TO ENTER : ";
  14.             cin>>counter;
  15.             cout<<"\nENTER THE ELEMENTS OF THE ARRAY : ";
  16.             for (index=0;index<counter;index++)
  17.             {
  18.                 cout<<"\nENTER ELEMENT "<<index+1<<" : ";
  19.                 cin>>element[index];
  20.             }
  21.             start=0;
  22.             last=counter-1;
  23.         }
  24.        
  25.         void bubble()       //function sorts the elements of array using bubble sort
  26.         {
  27.             int index = 0,index2 = 0,temp = 0;
  28.             for(index = 0; index < counter-1; index++)
  29.             {
  30.                 for(index2 = 0; index2 < counter-1; index2++)
  31.                 {
  32.                     if(element[index2] > element[index2+1])
  33.                     {
  34.                         temp = element[index2];
  35.                         element[index2] = element[index2+1];
  36.                         element[index2+1] = temp;
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.  
  42.         void binarySearch(int num)   //function searches the element using binary search
  43.         {  
  44.             int low = start, high = last, position = 0, mid = 0;
  45.             bool found = false;
  46.  
  47.             // loop till high is greater than low or till the element is not found
  48.             while (low <= high && found == false) {
  49.  
  50.                 // calculate the mid value
  51.                 mid = (low + high) / 2;
  52.  
  53.                 // compare the mid value with the element
  54.                 // if equal then print the position and return
  55.                 if (element[mid] == num) {
  56.                     position = mid;
  57.                     found = true;
  58.                     break;
  59.                 }
  60.  
  61.                 // else if the middle value if greater than the element
  62.                 // then element lies on the left side of the mid so decrease high value
  63.                 else if (element[mid] > num) {
  64.                     high = mid - 1;
  65.                 }
  66.  
  67.                 // else if the middle value if less than the element
  68.                 // then element lies on the right side of the mid so increase low value
  69.                 else if (element[mid] < num) {
  70.                     low = mid + 1;
  71.                 }
  72.             }
  73.             // display this if element not found
  74.             if (found == false) {
  75.                 cout<<"Element not Found in the Array";
  76.                 return;
  77.             }
  78.             cout<<"Element found at position "<<mid + 1;
  79.             return;
  80.         }
  81.  
  82. };
  83.  
  84. int main()
  85. {
  86.     Search s1;
  87.     int cont=0,element=0;
  88.     s1.getdata();
  89.     s1.bubble();
  90.     cout<<"\nTHE ELEMENTS OF THE ARRAY HAVE BEEN SORTED ";
  91.     do
  92.     {
  93.         cout<<"\nENTER THE ELEMENT TO BE SEARCHED : ";
  94.         cin>>element;
  95.         flag=0;
  96.         s1.binarySearch(element);
  97.         cout<<"\nDO YOU WANT TO CHECK AGAIN (1/0):";
  98.         cin>>cont;
  99.     }while(cont==1);
  100.     return 0;
  101. }
  102.  
  103. /******************************OUTPUT**********************************
  104.  
  105. HOW MANY ELEMENTS DO YOU WANT TO ENTER : 10
  106.  
  107. ENTER THE ELEMENTS OF THE ARRAY :
  108. ENTER ELEMENT 1 : 45
  109.  
  110. ENTER ELEMENT 2 : 95
  111.  
  112. ENTER ELEMENT 3 : 75
  113.  
  114. ENTER ELEMENT 4 : 15
  115.  
  116. ENTER ELEMENT 5 : 35
  117.  
  118. ENTER ELEMENT 6 : 62
  119.  
  120. ENTER ELEMENT 7 : 2
  121.  
  122. ENTER ELEMENT 8 : 98
  123.  
  124. ENTER ELEMENT 9 : 48
  125.  
  126. ENTER ELEMENT 10 : 12
  127.  
  128. THE ELEMENTS OF THE ARRAY HAVE BEEN SORTED
  129. ENTER THE ELEMENT TO BE SEARCHED : 12
  130.  
  131.  
  132. THE ELEMENT 12 HAS BEEN FOUND ON 1 POSITION
  133. DO YOU WANT TO CHECK AGAIN (1/0):1
  134.  
  135. ENTER THE ELEMENT TO BE SEARCHED : 100
  136.  
  137.  
  138. THE ELEMENT 100 WAS NOT FOUND
  139. DO YOU WANT TO CHECK AGAIN (1/0):1
  140.  
  141. ENTER THE ELEMENT TO BE SEARCHED : 1
  142.  
  143.  
  144. THE ELEMENT 1 WAS NOT FOUND
  145. DO YOU WANT TO CHECK AGAIN (1/0):1
  146.  
  147. ENTER THE ELEMENT TO BE SEARCHED : 48
  148.  
  149.  
  150. THE ELEMENT 48 HAS BEEN FOUND ON 5 POSITION
  151. DO YOU WANT TO CHECK AGAIN (1/0):0
  152.  
  153. Process returned 0 (0x0)
  154. Press any key to continue.
  155.  
  156. */
Add Comment
Please, Sign In to add comment