Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int a[10];
- void inputArray()
- {
- int i;
- printf("Enter 10 elements:-\n");
- for(i = 0; i<10; i++)
- scanf("%d", &a[i]);
- }
- void bSort()
- {
- int i,j,temp;
- for(i = 10-2; i>=0; i--)
- for(j = 0; j <= i; j++)
- if (a[j] > a[j+1])
- {
- temp = a[j];
- a[j] = a[j+1];
- a[j+1] = temp;
- }
- }
- void showArray()
- {
- int i;
- printf("The array in ascending order is:-\n");
- for(i=0; i<10; i++)
- printf("| %d ",a[i]);
- }
- int inputElement()
- {
- int x;
- printf("|\nEnter the element to be searched for: ");
- scanf("%d",&x);
- return x;
- }
- int bSearch(int x, int l, int h)
- {
- int c = -1; int mid;
- if(l <= h)
- {
- mid = (l+h)/2;
- if(x == a[mid])
- return mid;
- if(x < a[mid])
- return bSearch(x, l, mid-1);
- else
- return bSearch(x, mid+1, h);
- }
- else
- return c;
- }
- void result(int c)
- {
- if (c == -1)
- printf("Not found!");
- else
- printf("Found at %d!", c);
- }
- int main()
- {
- int c;
- inputArray();
- bSort();
- showArray();
- c = inputElement();
- c = bSearch(c, 0, 10-1);
- result(c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement