Bubble Sort
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,a[100],temp;
clrscr();
printf("How many numbers, you input in array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\na[%d] = ",i+1);
scanf("%d",&a[i]);
}
for(j=n-1;j>0;j--)
for(i=0;i<j;i++)\
if(a[i]<a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
printf("\nAfter Sorting, Array contains : ");
for(i=0;i<n;i++)
printf("\n\na[%d] = %4d",i+1,a[i]);
getch();
}
Insertion Sort
void main()
{
int n,a[100],i,j,k;
clrscr();
printf(" How many numbers.");
scanf("%d",&n);
printf("Enter %d numbers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
k=a[i+1];
j=i;
while(a[j]<k&&j>=0)
{
a[j+1]=a[j--];
}
a[j+1]=k;
// printf(" %d",j);
}
a[j+1]=k;
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
// printf("%d",n);
getch();
}
Selection Sort
void main()
{
int n,a[100],i,j,k,temp;
clrscr();
printf(" How many numbers.");
scanf("%d",&n);
printf("Enter %d numbers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
k=i;
for(j=i;j<n;j++)
{
if(a[j]>a[k])
k=j;
}
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
// printf("%d",n);
getch();
}
Merge Sort
#include<stdio.h>
void getdata(int arr[],int n)
{
int i;
printf("enter the data:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
}
void display(int arr[],int n)
{
int i;
printf("");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
getchar();
}
void sort(int arr[],int low,int mid,int high)
{
int i,j,k,l,b[20];
l=low;
i=low;
j=mid+1;
while((l<=mid)&&(j<=high))
{
if(arr[l]>=arr[j])
{
b[i]=arr[l];
l++;
}
else
{
b[i]=arr[j];
j++;
}
i++;
}
if(l>mid)
{
for(k=j;k<=high;k++)
{
b[i]=arr[k];
i++;
}
}
else
{
for(k=l;k<=mid;k++)
{
b[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++)
{
arr[k]=b[k];
}
}
void partition(int arr[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
sort(arr,low,mid,high);
}
}
void main()
{
int arr[20];
int n;
printf("Enter number of data:");
scanf("%d",&n);
getdata(arr,n);
partition(arr,0,n-1);
display(arr,n);
getchar();
}
///////////////////////////////////////////////////////////////////////////
Copy & paste this code in your TC & run, then you will get output.......
If you have any problem please comment below.........
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,a[100],temp;
clrscr();
printf("How many numbers, you input in array : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\na[%d] = ",i+1);
scanf("%d",&a[i]);
}
for(j=n-1;j>0;j--)
for(i=0;i<j;i++)\
if(a[i]<a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
printf("\nAfter Sorting, Array contains : ");
for(i=0;i<n;i++)
printf("\n\na[%d] = %4d",i+1,a[i]);
getch();
}
Insertion Sort
void main()
{
int n,a[100],i,j,k;
clrscr();
printf(" How many numbers.");
scanf("%d",&n);
printf("Enter %d numbers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
k=a[i+1];
j=i;
while(a[j]<k&&j>=0)
{
a[j+1]=a[j--];
}
a[j+1]=k;
// printf(" %d",j);
}
a[j+1]=k;
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
// printf("%d",n);
getch();
}
Selection Sort
void main()
{
int n,a[100],i,j,k,temp;
clrscr();
printf(" How many numbers.");
scanf("%d",&n);
printf("Enter %d numbers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
k=i;
for(j=i;j<n;j++)
{
if(a[j]>a[k])
k=j;
}
temp=a[i];
a[i]=a[k];
a[k]=temp;
}
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
// printf("%d",n);
getch();
}
Merge Sort
#include<stdio.h>
void getdata(int arr[],int n)
{
int i;
printf("enter the data:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
}
void display(int arr[],int n)
{
int i;
printf("");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
getchar();
}
void sort(int arr[],int low,int mid,int high)
{
int i,j,k,l,b[20];
l=low;
i=low;
j=mid+1;
while((l<=mid)&&(j<=high))
{
if(arr[l]>=arr[j])
{
b[i]=arr[l];
l++;
}
else
{
b[i]=arr[j];
j++;
}
i++;
}
if(l>mid)
{
for(k=j;k<=high;k++)
{
b[i]=arr[k];
i++;
}
}
else
{
for(k=l;k<=mid;k++)
{
b[i]=arr[k];
i++;
}
}
for(k=low;k<=high;k++)
{
arr[k]=b[k];
}
}
void partition(int arr[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
partition(arr,low,mid);
partition(arr,mid+1,high);
sort(arr,low,mid,high);
}
}
void main()
{
int arr[20];
int n;
printf("Enter number of data:");
scanf("%d",&n);
getdata(arr,n);
partition(arr,0,n-1);
display(arr,n);
getchar();
}
///////////////////////////////////////////////////////////////////////////
Copy & paste this code in your TC & run, then you will get output.......
If you have any problem please comment below.........