C Program to Search Sorted Array using Binary Search

This is a C Program to implement Binary Search Algorithm.

Problem Description

We have to create a C Program which uses Binary search algorithm to predict that the element is present or not in the array. The user has to input a sorted array because binary search works on sorted array.

Expected Input and Output

1. Average Case: When the element to be searched is other than the middle element. For example:

If the input array is {1, 2, 3, 4, 5, 6}
and the key to be searched for is 6
then the expected output will be "Search Successful".

Average case time complexity: O(log n).

2. Best Case: If the element to be searched is the middle element of the array. For example:

advertisement
advertisement
If the input array is {1, 5, 9}
and the key to be searched is 5,
then the expected output will be "Search Successful".

Best case time complexity: O(1).

3. Worst Case: If the element to be searched for is not present in the array.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If the input array is {1, 3, 6, 8, 9}
and the key to be searched for is 4,
then the expected output will be "Search Unsuccessful".

Worst case time complexity: O(log n).

Problem Solution

1. We will be having an array of numbers, we just need to find out whether the element is present in the array or not.
2. It can be done using Linear Search but it takes up a lot of time, so we need a better searching algorithm that performs the same task but in less time in comparison to Linear Search.
3. In worst case Linear Search takes O(n) time, but Binary Search takes O(log n) time in worst case.

Program/Source Code

Here is source code of the C Program to find whether the element is present in the array or not using Binary Search Algorithm. The program is successfully compiled and tested using Codeblocks gnu/gcc compiler on Windows 10. The program output is also shown below.

advertisement
  1. /*
  2.  * C program to accept N numbers sorted in ascending order
  3.  * and to search for a given number using Binary Search.
  4.  * Report success or failure.
  5.  */
  6. #include <stdio.h>
  7.  
  8. void main()
  9. {
  10.     int array[10];
  11.     int i, j, num, temp, keynum;
  12.     int low, mid, high; 
  13.     printf("Enter the value of num \n");
  14.     scanf("%d", &num);
  15.     printf("Enter the elements one by one \n");
  16.     for (i = 0; i < num; i++)
  17.     {
  18.         scanf("%d", &array[i]);
  19.     }
  20.     printf("Input array elements \n");
  21.     for (i = 0; i < num; i++)
  22.     {
  23.         printf("%d\n", array[i]);
  24.     }
  25.     /*  Bubble sorting begins */
  26.     for (i = 0; i < num; i++)
  27.     {
  28.         for (j = 0; j < (num - i - 1); j++)
  29.         {
  30.             if (array[j] > array[j + 1])
  31.             {
  32.                 temp = array[j];
  33.                 array[j] = array[j + 1];
  34.                 array[j + 1] = temp;
  35.             }
  36.         }
  37.     }
  38.     printf("Sorted array is...\n");
  39.     for (i = 0; i < num; i++)
  40.     {
  41.         printf("%d\n", array[i]);
  42.     }
  43.     printf("Enter the element to be searched \n");
  44.     scanf("%d", &keynum);
  45.     /*  Binary searching begins */
  46.     low = 1;
  47.     high = num;
  48.     do
  49.     {
  50.         mid = (low + high) / 2;
  51.         if (keynum < array[mid])
  52.             high = mid - 1;
  53.         else if (keynum > array[mid])
  54.             low = mid + 1;
  55.     } while (keynum != array[mid] && low <= high);
  56.     if (keynum == array[mid])
  57.     {
  58.         printf("SEARCH SUCCESSFUL \n");
  59.     }
  60.     else
  61.     {
  62.         printf("SEARCH FAILED \n");
  63.     }
  64. }
Program Explanation

1. A Binary Search is a quick and efficient method of finding a specific target value from a set of ordered items. A Binary search is also known as a half-interval search.
2. Here in this program, the element to be searched is denoted by keynum.
3. In order to apply Binary Search, we need to have a sorted array. In the above program, we have used Bubble Sort before applying Binary Search.
4. After sorting the elements in ascending order, we have applied a binary search by first comparing the keynum with the middle element of the array.
5. If the value of keynum is equal to the middle element of the array, we have simply printed “SEARCH SUCCESSFUL”.
6. If keynum is other than the middle element of the array, we divide the array into two parts one having all the elements less than the middle element and the other having all the elements greater than the middle element, and start locating for keynum in the desired part.
7. If keynum is greater than the middle element, the value of low (starting index of the array), will become mid+1 and if it is less than the middle element high (last index of the array), will become mid-1. “mid” is the middle index of the array.
8. When we are modifying the values of low and high, we are basically dividing the array into two halves, and then looking for keynum in respective half.
9. The Best case time complexity of Binary Search is O(1). The only condition for implementing Binary Search is that the array should be sorted.

Runtime Test Cases
1. Enter the value of num
   6
   Enter the elements one by one
   1
   2
   3
   4
   5
   6
   Input array elements
   1
   2
   3
   4
   5
   6
   Sorted array is...
   1
   2
   3
   4
   5
   6
   Enter the element to be searched
   6
   SEARCH SUCCESSFUL
 
2. Enter the value of num
   3
   Enter the elements one by one
   1
   5
   9
   Input array elements
   1
   5
   9
   Sorted array is...
   1
   5
   9
   Enter the element to be searched
   5
   SEARCH SUCCESSFUL
 
3. Enter the value of num
   5
   Enter the elements one by one
   1
   3 
   6
   8
   9
   Input array elements
   1
   3
   6
   8
   9
   Sorted array is...
   1
   3
   6
   8
   9
   Enter the element to be searched
   4  
   SEARCH FAILED

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Arrays, go to C Programming Examples on Arrays. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.