1) / Program To Perform Linear Search
1) / Program To Perform Linear Search
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[20],i,key,n,flag=0;
clrscr( );
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("elements of the array are\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
printf("Enter the key element to be searched\n");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
flag=1;
break;
}
}
if(flag==1)
printf("Element found at position %d",i+1);
else
printf("Element not found");
getch( );
}
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[50],temp,i,n,loc,j,pos;
clrscr( );
printf("Enter the number of elements in the array\n");
scanf("%d",&n);
printf("Enter the elements for the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n Elements of array before sorting are \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
if(a[pos]>a[j]) //find location of the smallest element
pos=j;
temp=a[i]; //Move smallest element to the start of array
a[i]=a[pos];
a[pos]=temp;
}
printf("\n Elements of array in after sorting are \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch( );
}
5 /*program to perform insertion sort*/
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[20],i,t,n,j;
clrscr( );
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("elements of the array before sorting are\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
for(i=1; i < n; i++)
{
t = a[i];
for(j=i-1; (j >= 0) && (t < a[j]); j--)
a[j+1] = a[j];
a[j+1] = t;
}
printf("elements of the array after sorting are\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch( );
}