Thank you all the same !
我有个程序不太清楚,请高手指点!或回复我的邮箱lt_0453@163.com
#include<stdio.h>
#include<string.h>
void bubblesort(int *list, int index)
{
int i,j;
int change; //记录数值是否有交换位置
int temp; //数值交换时的暂存变量
while (change)
{
change =1;
for(j=index; j>0; j--) //外层循环
{
for(i=0; i<j-1; i++) //内层循环
{
if(list[i] > list[i+1]) //前者比后者大
{
temp = list[i+1]; //两数值交换
list[i+1] = list[i];
list[i] = temp;
change =0;
printf("\n current sorting result:");
for(i=0; i<index; i++)
printf("%d", list[i]);
}
}
}
}
}
//主程序:输入数值——》进行冒泡排序——》打印结果
void main()
{
int list[20];
int i, index;
int node;
printf("\n please input the value you want to sort(exit for 0):\n");
scanf("%d", &node);
while(node!=0)
{
list[index] =node;
index =index+1;
scanf("%d", &node);
}
bubblesort(list ,index);
printf("\n final sorting result:");
for(i=0;i<index; i++)
{
printf("%d", list[i]);
}
}
/*
please input the value you want to sort(exit for 0):
32 18 41 23 2 56 36 67 0
current sorting result:18 32 41 23 2 56 36 67
current sorting result:18 32 23 41 2 56 36 67
current sorting result:18 23 32 41 2 56 36 67
current sorting result:18 23 32 2 41 56 36 67
current sorting result:18 23 2 32 41 56 36 67
current sorting result:18 2 23 32 41 56 36 67
current sorting result:2 18 23 32 41 56 36 67
current sorting result:2 18 23 32 41 36 56 67
current sorting result:2 18 23 32 36 41 56 67
final sorting result:2 18 23 32 36 41 56 67
*/