Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* program to search a element using binary search in an array*/
- #include<stdio.h>
- #include<conio.h>
- void binarySearch(int,int);
- void sort(int);
- int element[100];
- int main()
- {
- int cont=0,ele=0,index=0,count=0;
- printf("\nHOW MANY ELEMENTS DO YOU WANT TO ENTER : ");
- scanf("%d",&count);
- printf("\nENTER THE ELEMENTS OF THE ARRAY : ");
- for (index = 0; index < count; index++)
- {
- printf("\nENTER ELEMENT %d : ",index+1);
- scanf("%d",&element[index]);
- }
- sort(count);
- printf("\n\nTHE ARRAY ELEMENTS HAVE BEEN SORTED ");
- do
- {
- printf("\nENTER THE ELEMENT TO BE SEARCHED : ");
- scanf("%d",&ele);
- binarySearch(ele,count);
- printf("\n\nDO YOU WANT TO FIND ANOTHER ELEMENT : ");
- scanf("%d",&cont);
- }while(cont==1);
- return 0;
- }
- void sort(int count)
- {
- int index = 0, index2 = 0,temp = 0;
- for(index = 0; index < count-1; index++)
- {
- for(index2 = 0; index2 < count-1; index2++)
- {
- if(element[index2] > element[index2+1])
- {
- temp = element[index2];
- element[index2] = element[index2+1];
- element[index2+1] = temp;
- }
- }
- }
- }
- void binarySearch(int num, int count) //function searches the element using binary search
- {
- int low = 0, high = count, position = 0, mid = 0;
- int found = 0;
- // loop till high is greater than low or till the element is not found
- while (low <= high && found == 0) {
- // calculate the mid value
- mid = (low + high) / 2;
- // compare the mid value with the element
- // if equal then print the position and return
- if (element[mid] == num) {
- position = mid;
- found = 1;
- break;
- }
- // else if the middle value if greater than the element
- // then element lies on the left side of the mid so decrease high value
- else if (element[mid] > num) {
- high = mid - 1;
- }
- // else if the middle value if less than the element
- // then element lies on the right side of the mid so increase low value
- else if (element[mid] < num) {
- low = mid + 1;
- }
- }
- // display this if element not found
- if (found == 0) {
- printf("Element not Found in the Array");
- return;
- }
- printf("Element found at position %d", mid + 1);
- return;
- }
- /******************************OUTPUT**********************************
- HOW MANY ELEMENTS DO YOU WANT TO ENTER : 10
- ENTER THE ELEMENTS OF THE ARRAY :
- ENTER ELEMENT 1 : 12
- ENTER ELEMENT 2 : 45
- ENTER ELEMENT 3 : 78
- ENTER ELEMENT 4 : 32
- ENTER ELEMENT 5 : 65
- ENTER ELEMENT 6 : 98
- ENTER ELEMENT 7 : 42
- ENTER ELEMENT 8 : 62
- ENTER ELEMENT 9 : 75
- ENTER ELEMENT 10 : 95
- THE ARRAY ELEMENTS HAVE BEEN SORTED
- ENTER THE ELEMENT TO BE SEARCHED : 10
- THE ELEMENT 10 WAS NOT FOUND
- DO YOU WANT TO FIND ANOTHER ELEMENT : 1
- ENTER THE ELEMENT TO BE SEARCHED : 100
- THE ELEMENT 100 WAS NOT FOUND
- DO YOU WANT TO FIND ANOTHER ELEMENT : 1
- ENTER THE ELEMENT TO BE SEARCHED : 95
- THE ELEMENT 95 HAS BEEN FOUND ON 8 POSITION
- DO YOU WANT TO FIND ANOTHER ELEMENT : 1
- ENTER THE ELEMENT TO BE SEARCHED : 75
- THE ELEMENT 75 HAS BEEN FOUND ON 6 POSITION
- DO YOU WANT TO FIND ANOTHER ELEMENT : 0
- Process returned 0 (0x0)
- Press any key to continue.
- */
Add Comment
Please, Sign In to add comment