|
发表于 2004-4-20 23:37:09
|
显示全部楼层
mmpcfans:
下面就是你想要的冒泡演示程序代码:
#define NORMAL "\033[0m"
#define GREEN "\033[1;32m"
#define BLUE "\033[1;34m"
void bubble(int *numbers,int total);
void disp_demo(int *numbers,int total,int x,int y,int c);
/* -------------------------------- */
main()
{
int i;
int test[10];
printf("Plase enter 10 numbers:\n");
for(i=0;i<10;i++)scanf("%d ",&test);
bubble(test,10);
disp_demo(test,10,-1,-1,-1);
printf(NORMAL "Bubble sort completed!\n");
}
/* -------------------------------- */
void bubble(int *numbers,int total)
{
int i,j,k,temp;
for(i=1;i<total;i++){
for(j=total-1;j>=i;--j){
disp_demo(numbers,10,j-1,j,0);
sleep(1);
if(numbers[j-1]>numbers[j]){
temp=numbers[j-1];
numbers[j-1]=numbers[j];
numbers[j]=temp;
disp_demo(numbers,10,j,j-1,1);
sleep(1);
}
}
}
}
/* -------------------------------- */
void disp_demo(int *numbers,int total,int x,int y,int c)
{
int i;
system("clear");
printf(NORMAL "Bubble demo:\n\n");
for(i=0;i<total;i++){
if(i==y && c==0)printf(GREEN "%d ",numbers);
else if(i==y && c==1){
printf(GREEN "%d",numbers);
printf(NORMAL "<->");
}
else if(i==x)printf(BLUE "%d ",numbers);
else printf(NORMAL "%d ",numbers);
}
printf("\n\n");
}
/* -------------------------------- */ |
|