C Program for Binary Search in an Array
C Program for Binary Search in an Array
A class stores five marks in their original order: 40, 35, 45, 35 and 50. The binary search task needs a clear rule about order, duplicates and how many elements exist.
Binary search eliminates an ordered range’s half after comparing its middle value with the target.
Before this exercise, review array elements and bounds and function parameters. The worked program below applies those ideas to this specific problem.
Explain
Explain why binary search uses this sequence of operations.
Trace
Trace the sample values and predict the printed result.
Apply
Search the original unsorted marks for missing value 99 using a linear scan, then compare the prerequisites of the two approaches.
Binary Search: follow the data
Given data
A class stores five marks in their original order: 40, 35, 45, 35 and 50. The binary search task needs a clear rule about order, duplicates and how many elements exist.
Rule
Binary search eliminates an ordered range’s half after comparing its middle value with the target.
Observed result
Found index: 3
Understand the operation
The separate ordered array satisfies the prerequisite; the original marks array does not. If the middle value is smaller than the target, only its right side can still contain that target.
The complete example knows its five-element bound. In a reusable function, pass an explicit element count with the array pointer; a pointer alone does not report that bound. This sample has two equal marks, which makes duplicate handling observable.
Read the complete C program
The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.
C17 · complete program
#include <stdio.h>
int main(void) {
int marks[] = {40, 35, 45, 35, 50};
int count = (int)(sizeof marks / sizeof marks[0]);
int ordered[] = {35, 35, 40, 45, 50};
int target = 45, low = 0, high = count - 1, found = -1;
while (low <= high) {
int middle = low + (high - low) / 2;
if (ordered[middle] == target) { found = middle; break; }
if (ordered[middle] < target) low = middle + 1;
else high = middle - 1;
}
printf("Found index: %d\n", found);
return 0;
}
Expected output
Found index: 3
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int marks[] = {40, 35, 45, 35, 50};
int count = (int)(sizeof marks / sizeof marks[0]);
int ordered[] = {35, 35, 40, 45, 50};
int target = 45, low = 0, high = count - 1, found = -1;
while (low <= high) {
int middle = low + (high - low) / 2;
if (ordered[middle] == target) { found = middle; break; }
if (ordered[middle] < target) low = middle + 1;
else high = middle - 1;
}
printf("Found index: %d\n", found);
return 0;
}
The first middle value is 40. Searching the remaining right side reaches 45 at index three.
Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.
Follow the worked calculation
Start with the sample
The first middle value is 40. Searching the remaining right side reaches 45 at index three.
Apply the rule
Compare the resulting output with the original sequence. A changed order and a changed set of values are different effects; decide which your operation promises.
Check the result
Found index: 3
Found index: 3
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
int main(void) {
int marks[] = {40, 35, 45, 35, 50};
int count = (int)(sizeof marks / sizeof marks[0]);
int ordered[] = {35, 35, 40, 45, 50};
int target = 45, low = 0, high = count - 1, found = -1;
while (low <= high) {
int middle = low + (high - low) / 2;
if (ordered[middle] == target) { found = middle; break; }
if (ordered[middle] < target) low = middle + 1;
else high = middle - 1;
}
printf("Found index: %d\n", found);
return 0;
}
Search the original unsorted marks for missing value 99 using a linear scan, then compare the prerequisites of the two approaches.
Worked answer
The linear scan is valid for the unsorted array and reports -1 after all five comparisons. Binary search needs an ordered sequence before it may eliminate a half.
C17 · complete program
#include <stdio.h>
int main(void) {
int marks[] = {40, 35, 45, 35, 50};
int count = (int)(sizeof marks / sizeof marks[0]);
int target = 99, found = -1;
for (int i = 0; i < count; i++) if (marks[i] == target) { found = i; break; }
printf("Found index: %d\n", found);
return 0;
}
Expected output
Found index: -1
Run in C compiler →Recognize the important boundaries
Unsorted data invalidates the elimination rule. With duplicates, this implementation returns one matching index, not necessarily the first or last.
The sample marks and their sums fit int. General input requires both valid array bounds and an arithmetic-range contract.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
Binary search eliminates an ordered range’s half after comparing its middle value with the target.
Continue with array indexes and bounds.
Continue with pointer and count contracts.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.