编写一个c语言程序,用选择法对10个数排序(由小到大)
时间: 2023-11-21 20:06:09 浏览: 42
```
#include <stdio.h>
int main() {
int arr[10] = {9, 5, 7, 2, 3, 1, 8, 4, 6, 0}; // 初始化一个数组
// 选择排序
for (int i = 0; i < 9; i++) {
int minIndex = i;
for (int j = i + 1; j < 10; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
// 输出排序后的结果
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
相关问题
编写一个c语言程序 要求输入十个整数,用选择法对十个整数从小到大排序
```c
#include <stdio.h>
int main() {
int nums[10]; // 存放十个整数的数组
int i, j, minIndex, temp;
printf("请输入十个整数:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &nums[i]);
}
// 选择排序
for (i = 0; i < 9; i++) {
minIndex = i;
for (j = i + 1; j < 10; j++) {
if (nums[j] < nums[minIndex]) {
minIndex = j;
}
}
temp = nums[i];
nums[i] = nums[minIndex];
nums[minIndex] = temp;
}
printf("排序后的结果为:\n");
for (i = 0; i < 10; i++) {
printf("%d ", nums[i]);
}
printf("\n");
return 0;
}
```
编写一个C语言程序:5个整数,要求用选择排序法对它们按由小到大的顺序排列。
好的,以下是符合您要求的C语言程序:
```c
#include <stdio.h>
void selection_sort(int arr[], int n);
int main()
{
int arr[5] = {5, 3, 7, 2, 1};
int n = 5;
selection_sort(arr, n);
printf("排序后的数组为:");
for(int i=0; i<n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
void selection_sort(int arr[], int n)
{
int min_index;
for(int i=0; i<n-1; i++)
{
min_index = i;
for(int j=i+1; j<n; j++)
{
if(arr[j] < arr[min_index])
{
min_index = j;
}
}
if(min_index != i)
{
int temp = arr[min_index];
arr[min_index] = arr[i];
arr[i] = temp;
}
}
}
```
输出结果为:
```
排序后的数组为:1 2 3 5 7
```
相关推荐
![](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)