C program for Bubble, Selection, Insertion and Heap Sorting
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void enterelements(int a[],int size)
{
int i;
for(i=1;i<=size;i++)
{
printf("\nEnter element%d: ",i);
scanf("%d",&a[i]);
}
}
void bubblesort(int a[],int size)
{
int i,j,temp;
for(i=1;i<=size;i++)
{
for(j=1;j<=size-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nSorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
}
void selectionsort(int a[],int size)
{
int i,j,temp;
for(i=1;i<=size;i++)
{
for(j=i+1;j<size+1;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nSorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
}
void insertionsort(int a[],int size)
{
int i,j,key;
for(i=2;i<=size;i++)
{
key=a[i];
j=i-1;
while((j>=1)&&(a[j]>key))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
}
printf("\nSorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
}
void buildheap(int a[],int b[],int size)
{
int i=1,j,temp;
while(i<=size)
{
b[i]=a[i];
j=i;
while(j>1)
{
if(b[j]>b[(int)floor(j/2)])
{
temp=b[j];
b[j]=b[(int)floor(j/2)];
b[(int)floor(j/2)]=temp;
j=(int)floor(j/2);
}
else
{
j=1;
}
}
i+=1;
}
}
void maxheap(int b[],int i,int n)
{
int great,temp,l,r;
l=2*i;
r=(2*i)+1;
if((l<=n)&&(b[l]>b[i]))
{
great=l;
}
else
{
great=i;
}
if ((r<=n)&&(b[r]>b[great]))
{
great=r;
}
if(great!=i)
{
temp=b[great];
b[great]=b[i];
b[i]=temp;
maxheap(b,great,n);
}
}
void heapsort(int a[],int size)
{
int i,b[100],c[100],temp;
buildheap(a,b,size);
for(i=size;i>=1;i--)
{
temp=b[1];
b[1]=b[i];
c[i]=temp;
if((i!=1)&&(1!=2))
{
maxheap(b,1,i-1);
}
}
printf("\nSorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",c[i]);
i=i+1;
}
}
int main()
{
int a[100],i,j,size,ch;
printf("\nEnter the size of array: ");
scanf("%d",&size);
do
{
printf("\n\n..MENU..\n1. Bubble sort\n2. Selection sort\n3. Insertion Sort\n4. Heap sort\n5. Exit\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: enterelements(a,size);
printf("\nUnsorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
bubblesort(a,size);
break;
case 2: enterelements(a,size);
printf("\nUnsorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
selectionsort(a,size);
break;
case 3: enterelements(a,size);
printf("\nUnsorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
insertionsort(a,size);
break;
case 4: enterelements(a,size);
printf("\nUnsorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
heapsort(a,size);
break;
case 5: exit(0);
default:printf("\nWrong choice!");
}
}while(1);
}
#include <stdlib.h>
#include <math.h>
void enterelements(int a[],int size)
{
int i;
for(i=1;i<=size;i++)
{
printf("\nEnter element%d: ",i);
scanf("%d",&a[i]);
}
}
void bubblesort(int a[],int size)
{
int i,j,temp;
for(i=1;i<=size;i++)
{
for(j=1;j<=size-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nSorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
}
void selectionsort(int a[],int size)
{
int i,j,temp;
for(i=1;i<=size;i++)
{
for(j=i+1;j<size+1;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nSorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
}
void insertionsort(int a[],int size)
{
int i,j,key;
for(i=2;i<=size;i++)
{
key=a[i];
j=i-1;
while((j>=1)&&(a[j]>key))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=key;
}
printf("\nSorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
}
void buildheap(int a[],int b[],int size)
{
int i=1,j,temp;
while(i<=size)
{
b[i]=a[i];
j=i;
while(j>1)
{
if(b[j]>b[(int)floor(j/2)])
{
temp=b[j];
b[j]=b[(int)floor(j/2)];
b[(int)floor(j/2)]=temp;
j=(int)floor(j/2);
}
else
{
j=1;
}
}
i+=1;
}
}
void maxheap(int b[],int i,int n)
{
int great,temp,l,r;
l=2*i;
r=(2*i)+1;
if((l<=n)&&(b[l]>b[i]))
{
great=l;
}
else
{
great=i;
}
if ((r<=n)&&(b[r]>b[great]))
{
great=r;
}
if(great!=i)
{
temp=b[great];
b[great]=b[i];
b[i]=temp;
maxheap(b,great,n);
}
}
void heapsort(int a[],int size)
{
int i,b[100],c[100],temp;
buildheap(a,b,size);
for(i=size;i>=1;i--)
{
temp=b[1];
b[1]=b[i];
c[i]=temp;
if((i!=1)&&(1!=2))
{
maxheap(b,1,i-1);
}
}
printf("\nSorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",c[i]);
i=i+1;
}
}
int main()
{
int a[100],i,j,size,ch;
printf("\nEnter the size of array: ");
scanf("%d",&size);
do
{
printf("\n\n..MENU..\n1. Bubble sort\n2. Selection sort\n3. Insertion Sort\n4. Heap sort\n5. Exit\n\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: enterelements(a,size);
printf("\nUnsorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
bubblesort(a,size);
break;
case 2: enterelements(a,size);
printf("\nUnsorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
selectionsort(a,size);
break;
case 3: enterelements(a,size);
printf("\nUnsorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
insertionsort(a,size);
break;
case 4: enterelements(a,size);
printf("\nUnsorted elements are: ");
i=1;
while(i<=size)
{
printf(" %d",a[i]);
i=i+1;
}
heapsort(a,size);
break;
case 5: exit(0);
default:printf("\nWrong choice!");
}
}while(1);
}
No comments