Bubble Sort
#include<stdio.h>
#include<conio.h>
void main()
{
int ar[5],i,j,flag,tmp;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter Number>> ");
scanf("%d",&ar[i]);
}
printf("\n\tOriginal Array\n");
for(i=0;i<5;i++)
{
printf("\t%d",ar[i]);
}
for(i=0;i<5;i++)
{
flag=0;
for(j=0;j<5;j++)
{
if(ar[j]>ar[j+1])
{
tmp=ar[j+1];
ar[j+1]=ar[j];
ar[j]=tmp;
flag=1;
}
}
if(flag==0)
break;
}
printf("\n\tAfter Sorting\n");
for(i=0;i<5;i++)
{
printf("\t%d",ar[i]);
}
getch();
}
Selection Sort
#include<stdio.h>#define max 5
void main()
{
int ar[max],i,j,p,tmp;
clrscr();
for(i=0,j=9;i<max;i++,j--)
{
printf("Enter %d'st number>> ",i+1);
scanf("%d",&ar[i]);
}
printf("\nBefore Soting\n\n");
for(i=0;i<max;i++)
printf("\t%d",ar[i]);
for(i=0;i<max-1;i++)
{
p=i;
for(j=i+1;j<max;j++)
{
if(ar[p]>ar[j])
p=j;
}
if(p!=i)
{
tmp=ar[i];
ar[i]=ar[p];
ar[p]=tmp;
}
}
printf("\n\nAfter Sorting \n\n");
for(i=0;i<max;i++)
printf("\t%d",ar[i]);
getch();
}
No comments:
Post a Comment