用冒泡法排序,对12个浮点数,这12个浮点数放在数组。从小到大的代码
时间: 2024-02-15 11:05:31 浏览: 61
冒泡排序法的代码。。
4星 · 用户满意度95%
好的,以下是用冒泡法排序对12个浮点数从小到大排列的代码示例:
```c
#include <stdio.h>
void bubble_sort(float arr[], int size) {
int i, j;
for (i = 0; i < size - 1; i++) {
for (j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
float temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
float arr[12] = {3.2, 1.5, 9.7, 4.6, 6.8, 2.3, 5.1, 0.9, 7.4, 8.6, 11.0, 10.2};
int size = sizeof(arr) / sizeof(float);
printf("Before sorting:\n");
for (int i = 0; i < size; i++) {
printf("%f ", arr[i]);
}
bubble_sort(arr, size);
printf("\nAfter sorting:\n");
for (int i = 0; i < size; i++) {
printf("%f ", arr[i]);
}
return 0;
}
```
输出结果为:
```
Before sorting:
3.200000 1.500000 9.700000 4.600000 6.800000 2.300000 5.100000 0.900000 7.400000 8.600000 11.000000 10.200000
After sorting:
0.900000 1.500000 2.300000 3.200000 4.600000 5.100000 6.800000 7.400000 8.600000 9.700000 10.200000 11.000000
```
阅读全文