C Program to Implement Sequential and Binary Search on Same Array

«
»
This C program Implements Sequential and Binary Search on Same Array.

Here is the source code of the C program to display search result using Sequential search and Binary search. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program for binary search and the sequential search on 
  3.  * the same sorted array
  4.  */
  5. #include <stdio.h>
  6.  
  7. /* Function for sequential search  */
  8. void sequential_search(int array[], int size, int n)
  9. {
  10.     int i;
  11.     for (i = 0; i < size; i++)
  12.     {
  13.         if (array[i] == n)
  14.         {
  15.             printf("%d found at location %d.\n", n, i+1);
  16.             break;
  17.         }
  18.     }
  19.     if (i == size)
  20.         printf("Not found! %d is not present in the list.\n", n);
  21. }
  22. /*  End of sequential_search()  */
  23.  
  24. /*  Function for binary search  */
  25. void binary_search(int array[], int size, int n)
  26. {
  27.     int i, first, last, middle;
  28.     first = 0;
  29.     last = size - 1;
  30.     middle = (first+last) / 2;
  31.  
  32.     while (first <= last) {
  33.         if (array[middle] < n)
  34.             first = middle + 1;
  35.         else if (array[middle] == n) {
  36.             printf("%d found at location %d.\n", n, middle+1);
  37.            break;
  38.         }
  39.         else
  40.             last = middle - 1;
  41.  
  42.         middle = (first + last) / 2;
  43.     }
  44.     if ( first > last )
  45.         printf("Not found! %d is not present in the list.\n", n);
  46. }
  47. /*  End of binary_search()  */
  48.  
  49. /*  The main() begins  */
  50. int main()
  51. {
  52.     int a[200], i, j, n, size;
  53.     printf("Enter the size of the list:");
  54.     scanf("%d", &size);
  55.     printf("Enter %d Integers in ascending order\n", size);
  56.     for (i = 0; i < size; i++)
  57.         scanf("%d", &a[i]);
  58.     printf("Enter value to find\n");
  59.     scanf("%d", &n);
  60.     printf("Sequential search\n");
  61.     sequential_search(a, size, n);
  62.     printf("Binary search\n");
  63.     binary_search(a, size, n);
  64.     return 0;
  65. }

$ gcc search.c
$ a.out
Enter the size of the list:10
Enter 10 Integers in ascending order
2 5 8 12 16 25 31 45 48 55
Enter value to find
16
sequential search
16 found at location 5.
Binary search
16 found at location 5.

Sanfoundry Global Education & Learning Series – 1000 C Algorithms.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C Algorithms and Solutions, go to C Algorithms.

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 & technical discussions at Telegram SanfoundryClasses.