## 09 May, 2013

### Various Sorting algorithm programs

#include<stdio.h>
void bubble(int a[],int n);
int i,j,temp,a[25],n;
void main()
{
clrscr();
printf("\n\nEnter how many numbers you want to sort\n\n");
scanf("%d",&n);
printf("\nEnter the numbers \n");
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
bubble(a,n);
printf("\n\nFinal sorted list is ");
for (i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
void bubble(int a[],int n)
{
int k;
for(i=0;i<n-1;i++)
{
printf("\n\n PASS->%d ",i+1);
for(j=i+1;j<=n-1;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for(k=0;k<n;k++)
printf("%d ",a[k]);
printf("\n");
}
}
#include<stdio.h>
#include<conio.h>
int n,i=0,j=0,t=0,k=0,a[30];
void main()
{
clrscr();
printf("\nEnter how many numbers you want to sort\n");
scanf("%d",&n);
printf("\nEntyer the numbers \n");
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for (i=1;i<n;i++)
{
printf("\n\nPASS %d-->",i);
t=a[i];
for(j=i-1;((j>=0)&&(t<a[j]));j--)
a[j+1]=a[j];
a[j+1]=t; //j decreases
for(k=0;k<n;k++)
printf("%d ",a[k]);
}
printf("\n\nThe sorted list is : ");
for (j=0;j<n;j++)
printf("%d ",a[j]);
getch();
}
#include <stdio.h>
#include<conio.h>

void insertion_sort(int x[],int length)
{
int key,i,j;
for(j=1;j<length;j++)
{
key=x[j];
i=j-1;
while(x[i]>key && i>=0)
{
x[i+1]=x[i];
i--;
}
x[i+1]=key;
}
}

void main()
{
void insertion_sort(int [],int);
int A[100];
int x=0,n=0;
clrscr();
printf("******INSERTION SORT******");
printf("\n\nENTER THE LIMIT : ");
scanf("%d",&n);
printf("\n\nENTER THE ELEMENTS ONE BY ONE\n\n");
for(x=0;x<n;x++)
scanf("%d",&A[x]);
printf("\n\nNON SORTED LIST\n\n");
for(x=0;x<n;x++)
{
printf("\n%d",A[x]);
}
insertion_sort(A,n);
printf("\n\nSORTED LIST\n\n");
for(x=0;x<n;x++)
{
printf("\n%d",A[x]);
}
getch();
}
#include<stdio.h>
#include<conio.h>

void merge_split(int a[],int first,int last);
void merge(int a[],int f1,int l1,int f2,int l2);
int a[25],b[25];

void main()
{
int i,n;
clrscr();
printf("\n\nMERGE SORT");
printf("\n\n*********");
printf("\n\nEnter the limit : ");
scanf("%d",&n);
printf("\nEnter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
merge_split(a,0,n-1);
printf("\n\nSorted list : ");
for(i=0;i<n;i++)
printf("\n %d",a[i]);
getch();
}

void merge_split(int a[],int first,int last)
{
int mid;
if(first<last)
{
mid=(first+last)/2;
merge_split(a,first,mid);
merge_split(a,mid+1,last);
merge(a,first,mid,mid+1,last);
}
}

void merge(int a[],int f1,int l1,int f2,int l2)
{
int i,j,k=0;
i=f1;
j=f2;
while(i<=l1 && j<=l2)
{
if(a[i]<a[j])
b[k]=a[i++];
else
b[k]=a[j++];
k++;
}
while(i<=l1)
b[k++]=a[i++];
while(j<=l2)
b[k++]=a[j++];
i=f1;
j=0;
while(i<=l2 && j<k)
a[i++]=b[j++];
}

#include<stdio.h>
#include<conio.h>
int i,j,n,pivot,a[20];
void quick(int a[],int left,int right);
void swap(int a[],int i,int j);
void main()
{
int n,a[20];
textcolor(15);
clrscr();
printf("\n\nQUICK SORT");
printf("\n\nEnter the limit : ");
scanf("%d",&n);
textcolor(4);
textcolor(5);
clrscr();
printf("\n\nEnter the element\n\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick(a,0,n-1);
textcolor(10);
printf("\n\nThe sorted list is \n\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
getch();
}
void quick(int a[],int first,int last)
{
if(first<last)
{
pivot=a[first];
i=first;
j=last;
while(i<j)
{
while(a[i]<=pivot&&i<last)
i++;
while(a[j]>=pivot&&j>first)
j--;
if(i<j)
swap(a,i,j);
}
swap(a,first,j);
quick(a,first,j-1);
quick(a,j+1,last);
}
}
void swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
#include <stdio. h >
#include<conio.h>
void shell(int a[],int n);
int i,j,k,n,temp,array[25];
void main()
{
clrscr();
printf("\n SHELL SORT");
printf("\n **********");
printf("\n Enter the limit:");
scanf("%d",&n);
printf("\n Enter the elements\n\n");
for(i=0;i<n;i++)
scanf("%d",&array[i]);
shell(array,n);
printf("\n Sorted list:");
for(i=0;i<n;i++)
printf("\n %d",array[i]);
getch();
}

void shell(int a[],int n)
{
for(i=(n+1)/2;i>=1;i/=2)
{
for(j=i;j<=n-1;j++)
{
temp=a[j];
k=j-i;
while(k>=0&&temp<a[k])
{
a[k+i]=a[k];
k=k-i;
}
a[k+i]=temp;
}
}
}