You are on page 1of 4

1.

Insertion sort code:

#include<stdio.h>
#include<stdlib.h>
void insertionsort(int [],int);
void insertionsort(int a[],int n)
{
int i,j,q,k;
for(i=1;i<n;i=i+1)
{
j=i;
while(j>0 && a[j]<a[j-1])
{
q=a[j];
a[j]=a[j-1];
a[j-1]=q;
j=j-1;
}
}
for(k=0;k<n;k=k+1)
{
printf("%d ",a[k]);
}
}
int main()
{
int i,n;
printf("Enter number of nodes to be entered\n");
scanf("%d\n",&n);
int a[111];
for(i=0;i<n;i=i+1)
{
scanf("%d",&a[i]);
}
insertionsort(a,n);
return 0;
}

2.SHELL SORT CODE:

#include<stdio.h>
#include<stdio.h>
void shell_sort(int [],int);
void shell_sort(int a[],int n)
{
int lp,rp,gap,t;
for(gap=n/2;gap>0;gap=gap/2)
{
for(rp=gap;rp<n;rp=rp+1)
{
for(lp=rp-gap;lp>=0;lp=lp-gap)
{
if(a[lp]>=a[lp+gap])
{
t=a[lp];
a[lp]=a[lp+gap];
a[lp+gap]=t;
}
}
}
}
}
int main()
{
int i,n;
printf("Enter number of elements need to be entered\n");
scanf("%d",&n);
int a[i];
printf("push the elements that need to be entered\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
shell_sort(a,n);
printf("The elements after sorted are:\n");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}

3.QUICK SORT CODE:

#include<stdio.h>
#include<stdio.h>
#include<limits.h>
void quick_sort(int [],int,int);
void quick_sort(int a[],int low,int high)
{
int i,j,p,t;
if(low<high)
{
i=low;
j=high;
p=low;
while(i<j)
{
while(i<=high && a[i]<=a[p])
i++;
while(j>low && a[j]>a[p])
j--;
}
if(i<j)
{
t=a[i];
a[i]=a[p];
a[p]=t;
}
t=a[j];
a[j]=a[p];
a[p]=t;
quick_sort(a,0,j-1);
quick_sort(a,j+1,high);
}
}
int main()
{
int i,n;
printf("Enter number of nodes to be entered\n");
scanf("%d",&n);
int a[i];
printf("Enter the elements that are pushed:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
quick_sort(a,0,n-1);
printf("Elements after rearrangement:\n");
for(i=0;i<n;i=i+1)
{
printf("%d ",a[i]);
}
return 0;
}

4.MERGE SORT CODE:

#include<stdio.h>
#include<limits.h>
void merge_Divide(int[], int, int);
void merge_Conquer(int[], int, int, int);
void merge_Divide(int a[], int low, int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
merge_Divide(a, low, mid);
merge_Divide(a, mid+1, high);
merge_Conquer(a, low, mid, high);
}
}
void merge_Conquer(int a[], int low, int mid, int high)
{
int i, j, k, l, m;
i=low;
j=mid+1;
k=0;
int t[100];
while(i<=mid && j<=high)
{
if(a[i]<=a[j])
{
t[k]=a[i];
k=k+1;
i=i+1;
}
else
{
t[k]=a[j];
k=k+1;
j=j+1;
}
}
while(i<=mid)
{
t[k]=a[i];
k=k+1;
i=i+1;
}
while(j<=high)
{
t[k]=a[j];
k=k+1;
j=j+1;
}
l=0;
m=low;
while(l<k && m<=high)
{
a[m]=t[l];
l=l+1;
m=m+1;
}
}
int main()
{
int i, n;
printf("Enter number of elements need to be entered:\n");
scanf("%d", &n);
int a[n];
printf("Enter the elements entered into the list:\n");
for(i=0; i<n; i=i+1)
{
scanf("%d", &a[i]);
}
merge_Divide(a,0,n-1);
for(i=0; i<n; i=i+1)
{
printf("%d ", a[i]);
}
return 0;
}

You might also like