Discussion Forums

New User? Please read instructions!

What is binary search? write a C program for binary search  

  RSS

thiyagarajan
(@thiyagarajan)
Eminent Member
Joined:2 years  ago
Posts: 26
27/01/2017 9:49 am  

What is binary search? write a C program for binary search


ReplyQuote
prakash
(@prakash)
Eminent Member
Joined:2 years  ago
Posts: 20
16/02/2017 2:47 pm  

C program for binary search:

 

#include <stdio.h>

int main()

{

     int c, n, first, last, mid, search, a[250];

     printf("Please enter number of elements\n");

     scanf("%d",&n);

 

     printf("Enter the elements one by one\n", n);

 

     for ( c = 0 ; c < n ; c++ )

     {

          scanf("%d",&a[c]);

     }

 

     printf("Enter the element to be searched\n");

     scanf("%d",&search);

 

     first = 0;

     last = n - 1;

     mid = (first+last)/2;

 

     while( first <= last )

     {

           if ( a[mid] < search )

          {

               first = mid + 1;    

          }

          else if ( a[mid] == search ) 

         {

               printf("%d is found at the location %d.\n", search, mid+1);

               break;

         }

         else

         {

              last = mid - 1;

         }

 

         mid = (first + last)/2;

     }

     if ( first > last )

         printf("Element %d is not found in the list\n", search);

 

     return 0;   

}

 

Output:

Please enter number of elements

3

Enter the elements one by one

5

3

8

Enter the element to be searched

3

3 is found at the location 2.

 


ReplyQuote
rameez
(@rameez)
New Member
Joined:1 year  ago
Posts: 1
26/07/2017 7:57 am  

I am using the following query and I would need help from professionals working in reputed enterprise grade software development companies so that I can get best examples of coding. The idea of getting help from professionals working in top firms is because it is a fact that experience teaches a lot of lessons and learning.

What is binary search?

In computer science, binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array.

Coding

 

/*

 * C program to accept N numbers sorted in ascending order

 * and to search for a given number using binary search.

 * Report success or failure.

 */

#include <stdio.h>

 

void 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]);

    }

    /*  Bubble sorting begins */

    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);

    /*  Binary searching begins */

    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");

    }

} $ cc pgm22.c

$ a.out

Enter the value of num

5

Enter the elements one by one

23

90

56

15

58

Input array elements

23

90

56

15

58

Sorted array is...

15

23

56

58

90

Enter the element to be searched

58

SEARCH SUCCESSFUL

 

$ a.out

Enter the value of num

4

Enter the elements one by one

1

98

65

45

Input array elements

1

98

65

45

Sorted array is...

1

45

65

98

Enter the element to be searched

6

SEARCH FAILED

ReplyQuote
premworld90
(@premworld90)
New Member
Joined:1 year  ago
Posts: 1
22/09/2017 5:57 pm  

Hi thiyagarajan,

Go here Binary Search Program in c


ReplyQuote
Share:

OTHER C PROGRAMS:

  1. C program for prime number
  2. C program for factorial
  3. C program for fibonacci series
  4. C program for palindrome
  5. C program for swapping 2 numbers with and without temp variables
  6. C program to find leap year
  7. C program to find armstrong number
  8. C program to find simple and compound interest
  9. C program to find largest of given 3 numbers
  10. C program to find smallest of given 3 numbers
  11. C program to convert lower case into upper case and vice versa
  12. C program to find sum and average of given 3 numbers
  13. C program to sum up all individual digits
  14. C program to reverse given number
  15. C program to reverse given string
  16. C program to find strong number
  17. C program to find square and cube of given number
  18. C program to print hello world without using semi colon
  19. C program to sort given names in alphabetical order
  20. C program to copy content of one file to another
  21. C program to sort given numbers in ascending order
  22. C program to sort given numbers in descending order
  23. C program to search given number in an array
  24. C program for recursive function
  25. C program for calculator application
  26. C program for bank application
  27. C program to check given number is perfect number or not
  28. C program to find array size
  29. C program to find whether given character vowel or not
  30. C program to check whether given number is positive or negative
  31. C program to find sum of n numbers
  32. C program to compare 2 numbers without if statement
  33. C program to generate random numbers
  34. C program to compare 2 arrays whether they are equal or not
  35. C program to print number from 1 to 500 without using any loop conditions
  36. C program to insert an element into an array
  37. C program to delete an element from an array
  38. C program to find hcf (gcd) and lcm
  39. C program to print diamond pattern
  40. C program to print pascal triangle
  41. C program to add two complex numbers
  42. C program for binary search
  43. C program for linear search
  44. C program for matrix addition
  45. C program for matrix multiplication
  46. C program for bubble sort
  47. C program for insertion sort
  48. C program for selection sort

Share this website !!!
Facebook Twitter Linkedin

  
Working

Please Login or Register