Professional Documents
Culture Documents
Advanced Sorting Methods
Advanced Sorting Methods
h>
#include<stdlib.h>
int a[5],n=5,b[5],o[5],c[20];
void accept(int a[5],int n)//enter 5 elements
{
int i;
for(i=0;i<n;i++)
{
printf("\n Enter element:: ");
scanf("%d",&a[i]);
}
}
while((i<=mid)&&(j<=high))
{
if(a[i]<=a[j])
{
b[k]=a[i];
k=k+1;
i=i+1;
}
else
{
b[k]=a[j];
k=k+1;
j=j+1;
}
}
while(i<=mid)
{
b[k]=a[i];
k=k+1;
i=i+1;
}
while(j<=high)
{
b[k]=a[j];
k=k+1;
j=j+1;
}
j=low;
k=0;
while(j<=high)
{
a[j]=b[k];
j=j+1;
k=k+1;
}
printf("\n*************After merge********");
display(a,n);
printf("\n*********************************");
}
/*
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
*/
int partition(int a[5],int n,int low,int high)
{
int down,temp,up,pivot;
up=high;
down=low+1;
pivot=a[low];
do
{
while((a[down]<pivot)&&(down<=high))
{
down++;
}
while((a[up]>pivot)&&(up>low))
{
up--;
}
if(down<up)
{
temp=a[down];
a[down]=a[up];
a[up]=temp;
}
}while(down<up);
a[low]=a[up];
a[up]=pivot;
return up;
}
if(low<high)
{
mid=(low+high)/2;
mergesort(a,b,low,mid,n);
mergesort(a,b,mid+1,high,n);
merge(a,b,low,mid,high,n);
}
printf("\n **************");
display(a,n);
printf("\n **************");
}
/*end of mergesort function*/
/*start of quickroot function*/
void quicksort(int a[5],int n,int low,int high)
{
int j;
printf("\n Inside quicksort:: ");
if(low<high)
{
j=partition(a,n,low,high);
printf("\n j=%d",j);
quicksort(a,n,low,j-1);
quicksort(a,n,j+1,high);
printf("\n***************");
display(a,n);
printf("\n***************");
}
}
/*end of quicksort function*/
/*start of counting_sort*/
void counting_sort(int a[5],int n)
{
int i,count=0,store,max,x,k,j,m=0;
max=a[0];
for(i=0;i<n;i++)
{
c[a[i]]=c[a[i]]+1;
}
printf("\n displaying c array:: ");
for(m=0;m<=max;m++)
printf("\n The element is :: %d",c[m]);
/* step no 4*/
for(i=1;i<=max;i++)
{
c[i]=c[i]+c[i-1];
}
printf("\n Array after cumulative addition is ::");
display(c,n);
/*end of step 4*/
/*step no 5:: */
for(i=n-1;i>=0;i--)
{
o[c[a[i]]]=a[i];
c[a[i]]=c[a[i]]-1;
}
printf("\n output array is :: ");
for(i=1;i<=n;i++)
printf("\n %d",o[i]);
}
/* end of counting_sort function*/
printf("\n******MENU******");
printf("\n 1.Mergesort");
printf("\n 2.Quickroot");
printf("\n 3.Counting sort");
printf("\n 4.Exit");
printf("\n Enter choice:: ");
scanf("%d",&ch);
switch(ch)
{
case 1://first case mergesort
accept(a,n);
display(a,n);
low=0;
high=n-1;
mergesort(a,b,low,high,n);
break;
case 4:exit(0);
}
}while(ch!=4);