What is Java Virtual Machine (JVM) ?
July 1, 2016Need PHP Developers at Kanwer Tech Ludhiana
July 1, 2016Program to Search From an Array using Binary Search in C
#include <stdio.h> void binary_search(); int a[50], n, item, loc, beg, mid, end, i; main() { printf(" Enter size of an array: "); scanf("%d", &n); printf(" Enter elements of an array in sorted form: "); for(i=0; i<n; i++) scanf("%d", &a[i]); printf(" Enter ITEM to be searched: "); scanf("%d", &item); binary_search(); getch(); } void binary_search() { beg = 0; end = n-1; mid = (beg + end) / 2; while ((beg<=end) && (a[mid]!=item)) { if (item < a[mid]) end = mid - 1; else beg = mid + 1; mid = (beg + end) / 2; } if (a[mid] == item) printf(" ITEM found at location %d", mid+1); else printf(" ITEM doesn't exist"); }
Example of Binary Search in C
Image by : Google Search