0% found this document useful (0 votes)
71 views

Specification: 5© C Program Program To Impliment Binary Search Algorithm

The document describes a C program that implements binary search. It includes the algorithm, flowchart, program code, procedure for executing the program, and sample input/output. The program takes an array as input, sorts it, and then uses binary search to check if a given number is present in the array. It returns whether the number is found or not found.

Uploaded by

prasad9440024661
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views

Specification: 5© C Program Program To Impliment Binary Search Algorithm

The document describes a C program that implements binary search. It includes the algorithm, flowchart, program code, procedure for executing the program, and sample input/output. The program takes an array as input, sorts it, and then uses binary search to check if a given number is present in the array. It returns whether the number is found or not found.

Uploaded by

prasad9440024661
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

RAGHU INSTITUTE OF TECHNOLOGY

SPECIFICATION:
5 C Program Program to impliment binary search
ALGORITHM:
Step 1: Start
Step 2: Declare integer variables a[10] ,I ,n, m, c=0, l, u, mid
Step 3: Read n
Step 4: i is initialized as 0and in steps of 1 do the condition i<n is checked until the it fails
Step 4.1 : read a[i]
Step 5: Read m
Step 6: Initialize l=0 , u=n-1
Step 7: while(l<=u)
Do
Step 7.1: mid(l+u)/2
Step 7.2: check m== a[mid] if condition true then
Step 7.2.1: c1
Step 7.2.2: Stop
otherwise
step 7.3: check m<a[mid] if condition true then
Step 7.3.1: umid-1
otherwise
step 7.4: i mid+1
done
Step 8: check c==0 if condition is true
Step 8.1 : display he number is not found. Otherwise display number is found
Step 9: Stop
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

FLOWCHART:
START

read n
false
for i=0 in steps of 1 do where i<n
true
read a[i]

Read m

l0
un-1
false
while l<=u
true
mid(l+u)/2

false
C==0

false

true

The number is
found

m==a
[mid]
false

true
c 1

m<a[mid]

I mid+1

u mid-1
true

Department of Computer Science & Engg

stop

The number is
not found

RAGHU INSTITUTE OF TECHNOLOGY

PROGRAM
/* C Program to implement binary search */
Program name:
/* Done By : C-Faculty
#include<stdio.h>
int main()
{
int a[10],i,n,m,c=0,l,u,mid;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements in ascending order: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
l=0,u=n-1;
while(l<=u){
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
break;
}
else if(m<a[mid]){
Department of Computer Science & Engg

// wk5c.c
Dated: 15/10/2013*/

RAGHU INSTITUTE OF TECHNOLOGY

u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");
return 0;
}
PROCEDURE FOR EXECUTING THE PROGRAM:
Step 1: After typing the program, press ESC button+shift+: and then type wq(to save the
program and quit)
Step 2: Now compile the program by using the following command
cc wk5c.c lcurses -lm
Step 3: Now go for running the program by using the command
./a.out

Step 4: To create an executing file use the command


cc wk5c.c -curses o binsearch

EXPECTED I/P AND O/P:


Output (1)
enter the size of an array 10
Enter the elements in ascending order 1 3 5 7 9 11 13 15 17 19
Enter the numbers to be searched 20
Department of Computer Science & Engg

RAGHU INSTITUTE OF TECHNOLOGY

The number is not found


Output (2)
enter the size of an array 4
Enter the elements in ascending order 1 4 7 6
Enter the numbers to be searched 1
The number is found
ORIGINAL OUTPUT :
Output (1)
enter the size of an array 5
Enter the elements in ascending order 2 4 6 8 10
Enter the numbers to be searched 6
The number is found
Output (2)
enter the size of an array 10
Enter the elements in ascending order 1 3 5 7 9 11 13 15 17 19
Enter the numbers to be searched 20
The number is not found

--xXx--

Department of Computer Science & Engg

You might also like