0% found this document useful (0 votes)
724 views3 pages

Linear Search Using C Program

The document discusses two algorithms for searching an element in an array: linear search and binary search. It provides code samples in C to implement linear search and binary search. For linear search, it iterates through the array and checks if each element is equal to the target element. For binary search, it recursively searches half of the remaining part of the sorted array after comparing the target element with the middle element of the current iteration. Both algorithms return whether the target element was found or not.

Uploaded by

kalanithi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
724 views3 pages

Linear Search Using C Program

The document discusses two algorithms for searching an element in an array: linear search and binary search. It provides code samples in C to implement linear search and binary search. For linear search, it iterates through the array and checks if each element is equal to the target element. For binary search, it recursively searches half of the remaining part of the sorted array after comparing the target element with the middle element of the current iteration. Both algorithms return whether the target element was found or not.

Uploaded by

kalanithi
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

LINEAR SEARCH USING C PROGRAM

1. Write a simple code for linear search in c


programming language
2. Wap a c program to search an element in an array
using linear search
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is found");
}

return 0;

Sample output:
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0
The number is found

BINARY SEARCH USING C PROGRAM

1. Write a simple code for binary search in c


programming language
2. Wap a c program to search an element in an array
using binary search
#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]){
u=mid-1;
}
else
l=mid+1;
}
if(c==0)
printf("The number is not found.");
else
printf("The number is found.");
}

return 0;

Sample output:
Enter the size of an array: 5
Enter the elements in ascending order: 4 7 8 11 21
Enter the number to be search: 11
The number is found.

You might also like