我正在使用以下代码进行选择排序,但它总是返回 0, 0, 0,....
#include<stdio.h>
main(){
int i,j;
int s=8;
float temp;
float a[8] = {99, 80.5, 66, 45, 02, 36, 45, 80};
for(i=0;i<s;i++){
for(j=i+1;j<s;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting is: ");
for(i=0;i<s;i++)
printf(" %d",a[i]);
return 0;
}
它适用于 int 变量但不适用于 float。怎么了?
最佳答案
排序(我猜它是某种冒泡排序算法)看起来不错。但是输出是错误的,您必须使用 %f
浮点值说明符而不是 %d
(用于整数)。
for(i=0;i<s;i++)
printf(" %f",a[i]);
关于c - c语言冒泡排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14053525/