c语言用选择法排序,对6个浮点数,这6个浮点数放在数组。从大到小
时间: 2024-02-24 22:58:49 浏览: 21
好的,针对您的问题,下面是C语言选择法排序的代码,能够对6个浮点数从大到小进行排序:
```c
#include <stdio.h>
int main()
{
float arr[6] = { 9.8, 2.6, 5.3, 1.2, 4.7, 3.0 };
int i, j, maxIndex;
float temp;
for (i = 0; i < 5; ++i)
{
maxIndex = i;
for (j = i + 1; j < 6; ++j)
{
if (arr[j] > arr[maxIndex])
{
maxIndex = j;
}
}
temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
}
printf("排序后的结果为:\n");
for (i = 0; i < 6; ++i)
{
printf("%.1f ", arr[i]);
}
return 0;
}
```
运行结果如下:
```
排序后的结果为:
9.8 5.3 4.7 3.0 2.6 1.2
```
希望能对您有所帮助!
相关问题
C语言用选择法排序,对6个浮点数,这6个浮点数放在数组。从大到小
选择排序是一种简单直观的排序算法,它的基本思想是每次从剩余未排序的元素中选择最小(或最大)的元素,并将其放到已排序的元素末尾。对于这道题目,我们可以使用选择排序将6个浮点数从大到小排序。具体实现代码如下:
```c
#include <stdio.h>
int main() {
float arr[6] = {6.8, 3.5, 9.2, 1.6, 2.7, 5.4};
int i, j, max_index;
float temp;
// 选择排序
for (i = 0; i < 5; i++) {
max_index = i;
for (j = i + 1; j < 6; j++) {
if (arr[j] > arr[max_index]) { // 找到最大值的下标
max_index = j;
}
}
if (max_index != i) { // 将最大值交换到已排序的元素末尾
temp = arr[i];
arr[i] = arr[max_index];
arr[max_index] = temp;
}
}
// 输出排序后的结果
printf("从大到小排序后的结果为:\n");
for (i = 0; i < 6; i++) {
printf("%f ", arr[i]);
}
printf("\n");
return 0;
}
```
这里我们先定义了一个长度为6的浮点数数组arr,并将其初始化为{6.8, 3.5, 9.2, 1.6, 2.7, 5.4},然后使用选择排序将其从大到小排序,并输出排序后的结果。
c语言用选择法排序,对6个浮点数,这6个浮点数放在数组。从大到小代码
好的,以下是用选择排序法对6个浮点数进行从大到小排序的代码示例:
```c
#include <stdio.h>
void selectionSort(float arr[], int n)
{
int i, j, maxIndex;
float temp;
for (i = 0; i < n - 1; i++)
{
maxIndex = i;
for (j = i + 1; j < n; j++)
{
if (arr[j] > arr[maxIndex])
{
maxIndex = j;
}
}
temp = arr[i];
arr[i] = arr[maxIndex];
arr[maxIndex] = temp;
}
}
int main()
{
float arr[6] = {5.2, 7.9, 3.6, 9.0, 1.1, 4.8};
int n = 6;
int i;
printf("Original array: ");
for (i = 0; i < n; i++) {
printf("%.1f ", arr[i]);
}
selectionSort(arr, n);
printf("\nSorted array: ");
for (i = 0; i < n; i++) {
printf("%.1f ", arr[i]);
}
return 0;
}
```
输出结果为:
```
Original array: 5.2 7.9 3.6 9.0 1.1 4.8
Sorted array: 9.0 7.9 5.2 4.8 3.6 1.1
```
其中,`selectionSort` 函数实现了选择排序算法,每次找到未排序数组中最大的元素,放在已排序数组的末尾。`main` 函数中,先输出原始数组,然后调用 `selectionSort` 函数进行排序,最后再输出排好序的数组。
相关推荐
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)