m2skills

bsearch c

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