1. Write a C Program to implement Linear Search?
Program:
#include<stdio.h>
int linearsearch(int a[],int n,int key)
{
int i;
for(i=0;i<n;i++)
{
if(key==a[i])
{
return i;
}
}
return -1;
}
int main()
{
int a[100],n,i,p,key;
printf("\nHow many elements you want to store in the array n=\n");
scanf("%d",&n);
printf("\nenter the elements into the array=\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nenter the element you want to search key=\n");
scanf("%d",&key);
p=linearsearch(a,n,key);
if(p==-1)
{
printf("\nThe search is unsuccessfull\n");
}
else
{
printf("\nThe search is successfull\n");
printf("\n%d is found at location %d\n",key,p+1);
}
return 0;
}
Output:
How many elements you want to store in the array n=
5
enter the elements into the array=
100
110
120
130
140
enter the element you want to search key=
120
The search is successfull
120 is found at location 3
2. Write a C Program to implement Binary Search?
Program:
#include<stdio.h>
int binarysearch(int a[],int n,int key)
{
int low,high,mid;
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
return mid;
}
if(key<a[mid])
{
high=mid-1;
}
else
{
low=mid+1;
}
}
return -1;
}
int main()
{
int a[100],n,i,p,key;
printf("\nHow many elements you want to store in the array n=\n");
scanf("%d",&n);
printf("\nenter the elements into the array=\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nenter the element you want to search key=\n");
scanf("%d",&key);
p=binarysearch(a,n,key);
if(p==-1)
{
printf("\nThe search is unsuccessfull\n");
}
else
{
printf("\nThe search is successfull\n");
printf("\n%d is found at location %d\n",key,p+1);
}
return 0;
}
Output:
How many elements you want to store in the array n=
5
enter the elements into the array=
10
20
30
40
50
enter the element you want to search key=
40
The search is successfull
40 is found at location 4
3. Write a C Program to implement Insertion Sort?
Program:
#include<stdio.h>
int nrecinsort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++){
temp=a[i];
for(j=i;j>0&&a[j-1]>temp;j--){
a[j]=a[j-1];
}
a[j]=temp;
}
}
int main(){
int a[100],n,i,j,temp;
printf("enter size of the array=\n");
scanf("%d",&n);
printf("\nenter elements into array=\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
nrecinsort(a,n);
printf("Sorted array is as follows\n");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
return 0;
}
Output:
enter size of the array=
5
enter elements into array=
5
0
-1
34
24
Sorted array is as follows
-1 0 5 24 34
4. Write a C Program to implement Bubble Sort?
Program:
#include<stdio.h>
int bubsort(int a[], int n){
int temp,i,j;
for(i=0;i<=(n - 2);i++){
for(j=0;j<= (n - 2-i);j++){
if(a[j] > a[j+1]){
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
int main(){
int a[100],n,i;
printf("enter size of the array=\n");
scanf("%d",&n);
printf("\nenter elements into array=\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
bubsort(a,n);
printf("Sorted array is as follows\n");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
return 0;
}
Output:
enter size of the array=
5
enter elements into array=
0
-1
10
5
20
Sorted array is as follows
-1 0 5 10 20
5. Write a C Program to implement Selection Sort?
Program:
#include<stdio.h>
int selsort(int a[],int n){
int temp,i,j,min;
for(i=0;i<=(n-2);i++){
min=i;
for(j=i+1;j<=(n-1);j++){
if(a[j]<a[min]){
min=j;
}
}
if(min!=i){
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
}
int main(){
int a[100],n,i;
printf("enter size of the array=\n");
scanf("%d",&n);
printf("\nenter elements into array=\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
selsort(a,n);
printf("Sorted array is as follows\n");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
return 0;
}
Output:
enter size of the array= 5
enter elements into array=
-1
10
Sorted array is as follows
-1 0 2 9 10