I am using following code for selection sort but it always return 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;
}
It works fine with int variables but not with float. whats wrong?
The sorting (I guess its some sort of bubble sort algorithm) seems fine. The output is wrong though, you have to use the
%fspecifier for float values instead of%d(which is for integers).