Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Binary Searching
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<stdio.h>

// Binary Searching with C Language

void main()
{
int i, n, item, a[100];
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the element of the array: ");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}

printf("Enter the item to be searched: ");
scanf("%d", &item);

int mid, beg = 0, end = n -1;

while(beg <= end)
{
mid = (beg + end) / 2;
if(a[mid] == item)
{
printf("The item is present at position: %d", mid);
break;
}
else if(a[mid] < item)
{
beg = mid + 1;
}
else
{
end = mid - 1;
}
}

if(beg > end)
{
printf("Item not found!");
}
}