बिजनेस मार्केटिंग का लो कॉस्ट फंडा , अपने मोबाइल से ऑटो sms भेजकर मार्केटिंग करे विजिट करे http://autotextsms.com/ बिजनेस मार्केटिंग का लो कॉस्ट फंडा http://autotextsms.com/

Search This Blog

Translate

Showing posts with label C Program binary search. Show all posts
Showing posts with label C Program binary search. Show all posts

C Program to accept sorted array and search using binary search

C Program binary search,

C Program to accept sorted array and search using binary search


 #include <stdio.h>
int main()
{ int array[10]; int i, j, num, temp, keynum;
 int low, mid, high;
printf("Enter the value of num \n");
scanf("%d", &num);
 printf("Enter the elements one by one \n");
 for (i = 0; i < num; i++)
 {
 scanf("%d", &array[i]); }
 printf("Input array elements \n");
for (i = 0; i < num; i++)
 {
 printf("%d\n", array[i]); }
 for (i = 0; i < num; i++)
 { for (j = 0; j < (num - i - 1); j++)
 { if (array[j] > array[j + 1])
 { temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } }
 printf("Sorted array is...\n");
for (i = 0; i < num; i++)
 { printf("%d\n", array[i]); }
 printf("Enter the element to be searched \n");
scanf("%d", &keynum); low = 1; high = num;
 do { mid = (low + high) / 2;
if (keynum < array[mid])
 high = mid - 1;
else if (keynum >
 array[mid]) low = mid + 1; }
 while (keynum != array[mid] && low <= high);
 if (keynum == array[mid]) { printf("SEARCH SUCCESSFUL \n"); }
 else { printf("SEARCH FAILED \n"); }

return 0; }


C Program example List