Write a program to implement binary search in C language.

Tested on: Turbo C++ Version 4.0Time Complexity: O (log n)Space Complexity: O (1)Open Source: MIT
1
2
    #include <stdio.h>
3
    #include <conio.h>
4
    void main()
5
    {
6
        int a[10], n, i, beg = 0, end, mid, flag = 0, item;
7
        clrscr();
8
        printf("Enter the no. of the elements in the array: ");
9
        scanf("%d", &n);
10
        for (i = 0; i < n; i++)
11
        {
12
            printf("Enter %d elements: ", i + 1);
13
            scanf("%d", &a[i]);
14
        }
15
        printf("Enter the item for searching: ");
16
        scanf("%d", &item);
17
18
        end = n;
19
        while (beg <= end)
20
        {
21
            mid = (beg + end) / 2;
22
            if (item == a[mid])
23
            {
24
                flag = 1;
25
                break;
26
            }
27
            if (item > a[mid])
28
                beg = mid + 1;
29
            else
30
                end = mid - 1;
31
        }
32
        if (flag == 1)
33
            printf("%d element at position %d", item, mid + 1);
34
        else
35
            printf("%d element is not found");
36
    }
37
38
        

Output

1
2
    Enter the no. of the elements in the array: 5
3
    Enter 1 elements: 2
4
    Enter 2 elements: 3
5
    Enter 3 elements: 4
6
    Enter 4 elements: 5
7
    Enter 5 elements: 6
8
    Enter the item for searching: 5
9
    5 element at position 4
10
   

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

How to run binary search program in C-Language?

To run binary search program in C-Language, follow the below mentioned steps:

  1. Copy the Code: Press the copy button in the code block to copy the complete code.
  2. Open Turbo C++ Version 4: Open the Turbo C++ (Version 4) software on your system.
  3. Paste the code: Paste the copied code into Turbo C++’s editor.
  4. Compile the Code: Compile the program by pressing the Alt+F9 keys. This shows you the errors and warnings, if any.
  5. Run the code: Press the Ctrl+F9 keys to run the program if the program is successfully compiled.
  6. Enter the Input: Provide the required inputs or your desired inputs, if applicable.
  7. Receive the Result: Get the result of the program on the screen based on the provided inputs.

More about the Binary Search C language program

Aim: To search for a particular element with the help of binary search in a user-provided list.

Concepts used in this program

  • Array: To store the elements in a 1-D array.
  • Binary Search Algorithm: To divide the search area into 2 half parts. This is why it is called Binary search.
  • While loop: To continue the search.
  • Mid Calculation: To find out the index of the middle element in the search area.
  • If-Else: To compare the searching element with the middle element.
  • Flag Variable: To check whether the element is found or not.

Real-life applications

  • Database Indexing: To locate the record very fast, binary search is used.
  • Library Management Software: To search for any book from the sorted records
  • Dictionary Software: Because the dictionary words are arranged in sorted order, binary search is used in that case.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.