Google Search

Custom Search

Sunday, February 14, 2010

Visual Bubble Sort in C

#include
#include
#include

void object(int,int,int);
void bubble(int);
void flow(int,int);
void mixing(int,int);
int el[10];
void main()
{
int n,i,d=0,m=0;
clrscr();
printf("Enter the No of Elements : " );
scanf("%d",&n);
for(i=0;i
{
printf("Enter the %d Element : ",i+1);
scanf("%d",&el[i]);
}
initgraph(&d,&m,"");
settextstyle(3,0,1);
outtextxy(250,50,"GIVEN NUMBERS ");
// outtextxy(250,260," AFTER SORTING ");
// line(0,getmaxy()/2,getmaxx(),getmaxy()/2);
for(i=0;i
{
object(100+i*50,150,el[i]);
getch();
}
bubble(n);
/* for(i=0;i
{
object(100+i*50,350,el[i]);
getch();
}*/
getch();
}
void bubble(int n)
{
int i,j,temp;

for(i=0;i
for(j=i;j
if(el[i]>el[j+1])
{
flow(i,j+1);
temp=el[i];
el[i]=el[j+1];
el[j+1]=temp;
}
}
void object(int x,int y,int no)
{
char s[8];
sprintf(s,"%d",no);
circle(x,y,15);
settextstyle(2,0,6);
outtextxy(x-3,y-10,s);
}
void flow(int f,int s)
{
int i;
for(i=0;i<50;i++)
{
setcolor(WHITE);
object(100+f*50,150+i*4,el[f]);
delay(15);
setcolor(0);
object(100+f*50,150+i*4,el[f]);
}
setcolor(WHITE);
object(100+f*50,150+i*4,el[f]);
for(i=0;i<50;i++)
{
setcolor(WHITE);
object(100+s*50,150+i*4,el[s]);
delay(10);
setcolor(0);
object(100+s*50,150+i*4,el[s]);
}
setcolor(WHITE);
object(100+s*50,150+i*4,el[s]);
mixing(f,s);
for(i=50;i>0;i--)
{
setcolor(WHITE);
object(100+f*50,150+i*4,el[s]);
delay(10);
setcolor(0);
object(100+f*50,150+i*4,el[s]);
}
setcolor(WHITE);
object(100+f*50,150+i*4,el[s]);
for(i=50;i>0;i--)
{
setcolor(WHITE);
object(100+s*50,150+i*4,el[f]);
delay(15);
setcolor(0);
object(100+s*50,150+i*4,el[f]);
}
setcolor(WHITE);
object(100+s*50,150+i*4,el[f]);
}
void mixing(int f,int s)
{
int i;
for(i=0;i<(s-f)*50;i++)
{
setcolor(WHITE);
object(100+f*50+i,350,el[f]);
object(100+s*50-i,350,el[s]);
delay(20);
setcolor(0);
object(100+f*50+i,350,el[f]);
object(100+s*50-i,350,el[s]);
}
setcolor(WHITE);
object(100+f*50+i,350,el[f]);
object(100+s*50-i,350,el[s]);
}

No comments: