You are on page 1of 5

Heap Sort

#include <stdio.h>
#include <stdlib.h>
void HEAP_CON(int *, int , int );//This function constructs Heap
void HEAP_SORT(int *, int );// This functon sorts by deleting the ele
from heap
void main()
{
int n, i, *a;
printf("\n\n\t\tEnter your no of entries:");
scanf("%d", &n);
a= (int *)malloc(sizeof (int) * n);
printf("\n\n\t\t Enter the ele. to the tree :");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
HEAP_SORT(a, n);
printf("\n\n\t\t Ele. after sorting:");
for (i = 0; i < n; i++)
printf("%3d ", a[i]);
}
void HEAP_CON(int *a, int i, int n)
{
int l, Re, j; //Re->root ele.,i->Root loc,l->left child location
for (Re = a[i]; 2*i+1< n; i = l)
{
l=2*i+1;
if (l!= n-1 && a[l] < a[l+1])
l++;
if (Re < a[l])
a[i] = a[l];
else
break;
}
a[i] = Re;
}
void HEAP_SORT(int *a, int n)
{
int i, temp, j;
for (i = n/2; i >= 0; i--)
HEAP_CON(a, i, n);
for (i = n-1; i > 0; i--)
{
temp = a[0];
a[0] = a[i];
a[i] = temp;
HEAP_CON(a, 0, i);
}
}

Quick Sort
#include<stdio.h>
int partition(int *a,int l,int u)
{
int piv=u,i=l+1,j=u,temp;
if(l<u)
{
while(1)
{
while(a[j]>a[piv])
j--;
while(a[i]<=a[piv])
i++;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
break;
}
temp=a[piv];
a[piv]=a[j];
a[j]=temp;
}
return j;
}
void QSORT(int *a,int l,int u)
{int p;
if(l<u)
{
p=partition(a,l,u);
QSORT(a,l,p-1);
QSORT(a,p+1,u);
}
return ;
}
void main()
{
int *ptr,n,i;
clrscr();
printf("\n\n\t\t Enter the no of ele. to be sorted :");
scanf("%d",&n);
ptr=(int *)malloc(sizeof(int)*n);
if(ptr==NULL)
{
printf("\n\n\t\t Memory not allocated ");

exit(0);
}
printf("\n\n\t\t Enter the elements in to the list :");
for(i=0;i<n;i++)
scanf("%d",(ptr+i));
QSORT(ptr,0,n-1);
printf("\n\n\t Ele. after sorting are :");
for(i=0;i<n;i++)
printf("%5d",*(ptr+i));
}

SHELL SORT
void SHELL_SORT(int ,int a[ ]);
void main()
{
register int i;
auto int a[50],n;
clrscr();
printf("\n\n\t\t Enter the no. of ele. to be read in to the array :");
scanf("%d",&n);
if(n>50)
{
printf("\n\n\t\t Ele. cannot be read :");
exit(0);
}
printf("\n\n\t\t Enter the elements in to the array :");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
SHELL_SORT(n,a);
printf("\n\n\t\t Sorted elements are :");
for(i=0;i<n;i++)
printf("%5d",a[i]);
}
void SHELL_SORT(int n,int a[50])
{
register int i,j,g;
int key;
for(g=n/2;g>0;g=g/2)
{
for(i=g;i<n;i++)
{
key=a[i];
for(j=i;j>=g && key<a[j-g];j=j-g)
{
a[j]=a[j-g];
}
a[j]=key;
}
}
}

Merge Sort
void MERGE(int a[50],int m[50],int l,int c,int r)
{
int n=r-l+1,i=l,j=c,k=l;
for( ;i<=c&&j<=r;)
{
if(a[i]<a[j])
m[k++]=a[i++];
else
m[k++]=a[j++];
}
while(i<=c)
m[k++]=a[i++];
while(j<=r)
m[k++]=a[j++];
for(i=0;i<n;i++,r--)
a[r]=m[r];
}
void MERGE_SORT(int a[50],int l,int r)
{
int c,i,m[50];
if(l<r)
{
c=(l+r)/2;
MERGE_SORT(a,l,c);
MERGE_SORT(a,c+1,r);
MERGE(a,m,l,c+1,r);
}
}
void main()
{
int ptr[50],n,i;
clrscr();
printf("\n\n\t\t Enter the no of ele. to be sorted :");
scanf("%d",&n);
printf("\n\n\t\t Enter the elements in to the list :");
for(i=0;i<n;i++)
scanf("%d",(ptr+i));
MERGE_SORT(ptr,0,n-1);
printf("\n\n\t\t Ele. after sorting are :");
for(i=0;i<n;i++)
printf("%5d",*(ptr+i));
}

You might also like