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

1) / Program To Perform Linear Search

The document contains 5 C programs that implement different sorting algorithms: 1) Linear search to search an element in an array 2) Binary search to search an element in a sorted array 3) Bubble sort to sort elements in an array using bubble sort algorithm 4) Selection sort to sort elements in an array using selection sort algorithm 5) Insertion sort to sort elements in an array using insertion sort algorithm

Uploaded by

sanjeetha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

1) / Program To Perform Linear Search

The document contains 5 C programs that implement different sorting algorithms: 1) Linear search to search an element in an array 2) Binary search to search an element in a sorted array 3) Bubble sort to sort elements in an array using bubble sort algorithm 4) Selection sort to sort elements in an array using selection sort algorithm 5) Insertion sort to sort elements in an array using insertion sort algorithm

Uploaded by

sanjeetha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

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( );
}

2) /*program to perform binary search*/


#include<stdio.h>
#include<conio.h>
void main( )
{
int a[20],i,key,low,high,n,mid,flag=0;
clrscr( );
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of the array in ascending order\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); // read element to be searched
low=0; //assign values for low and high
high=n-1;
printf("low=%d high=%d",low,high);
while(low<=high) //if low>high element is not found
{
mid=(low+high)/2; //find mid of array
if(a[mid]= =key) //compare a[mid] with key
{
flag=1;
break;
}
else if(a[mid]>key) //Search in first half of array
high=mid-1;
else
low=mid+1; //Search in second half of array
}
if(flag= =1) // if flag is 1 then element is found
printf("Element found at position %d",mid+1);
else
printf("Element not found");
getch( );
}

3) /*program to perform bubble 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=0;i<n-1;i++) //Outer for loop for each pass
for(j=1;j<n-i;j++) //Sorting in each pass
{
if(a[j]<a[j-1]) //move largest element to the end of array
{
t=a[j];
a[j]=a[j-1];
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( );
}

4) /* program to perform selection sort*/

#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( );
}

You might also like