Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int flag=0;
- void binarysearch(int x, int a[], int m, int n)
- {
- int middle=(m+n)/2;
- if(a[middle]==x)
- {
- printf("%d has been found at postion %d!n", x, middle+1);
- flag=1;
- }
- else
- if(x > a[middle])
- binarysearch(x, a, middle, n);
- else
- if(x < a[middle])
- binarysearch(x, a, m, middle);
- }
- main()
- {
- int i, size, x;
- int a[100];
- printf("Enter the size of the list : ");
- scanf("%d", &size);
- printf("Enter the list items in ascending order : n");
- for (i=0; i<size; i++)
- scanf("%d", &a[i]);
- printf("Enter the element to be found : ");
- scanf("%d", &x);
- binarysearch(x, a, 0, size-1);
- if(flag != 1)
- printf("%d has not been found in the list!", x);
- }
- if(m == n)
- {
- if(a[n] == x) { printf("foundn"); }
- return;
- }
- int binarysearch(int x, int a[], int m, int n)
- {
- int middle=(m+n)/2;
- if(a[middle]==x)
- {
- printf("%d has been found at postion %d!n", x, middle+1);
- return middle;
- }
- else
- if(x > a[middle])
- return binarysearch(x, a, middle, n);
- else
- if(x < a[middle])
- return binarysearch(x, a, m, middle);
- //if it is not found in the whole array
- return -1;
- }
- void binarysearch(int x, int a[], int m, int n)
- {
- int middle=(m+n)/2;
- if(n == m && x != a[middle])
- {
- printf("%d is not in the array", x);
- return;
- }
- //...
- void binarysearch(int x, int a[], int m, int n)
- {
- int middle=(m+n)/2;
- if(n == m && x != a[middle])
- {
- printf("%d is not in the array", x);
- }
- else
- if(a[middle]==x)
- //...
Add Comment
Please, Sign In to add comment