Friday, 10 October 2014

Binary search with out using Recursion

#include <stdio.h> int b_search(int *a,int size,int key) { int first=0,last=size-1,mid; while(first<=last) { mid=(first+last)/2; if(a[mid]==key) return mid; if(a[mid]<key) first=mid+1; else last=mid-1; } return -1; } void main() { int arr[]={11,18,52,95,101,107,135,148,170,185}; printf("%d",b_search(arr,10,185)); }

No comments:

Post a Comment