用选择法对10个整数由小到大排序由c语言实现
时间: 2024-06-13 11:09:42 浏览: 21
以下是用选择法对10个整数由小到大排序的C语言实现代码:
```c
#include <stdio.h>
int() {
int a[10], i, j, min, temp;
scanf("%d %d %d %d %d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]);
for (i = 0; i < 9; i++) {
min = i;
for (j = i + 1; j < 10; j++) {
if (a[j] < a[min]) {
min = j;
}
}
if (min != i) {
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
for (i = 0; i < 10; i++) {
printf("%d ", a[i]);
}
return 0;
}
```
相关问题
C语言用选择法对10个整数由小到大排序
下面是用选择排序法对10个整数由小到大排序的C语言代码:
```c
#include <stdio.h>
int main() {
int arr[10] = {5, 2, 7, 3, 9, 1, 8, 4, 6, 0}; // 待排序数组
int i, j;
int minIndex, tmp;
// 选择排序
for (i = 0; i < 9; i++) {
minIndex = i;
for (j = i + 1; j < 10; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
tmp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = tmp;
}
}
// 输出排序结果
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
输出结果为:0 1 2 3 4 5 6 7 8 9
用C语言,用选择法对10个整数由小到大排序
下面是用C语言实现选择排序的代码,可以对10个整数由小到大进行排序:
```c
#include <stdio.h>
void selection_sort(int arr[], int n) {
int i, j, min_idx;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
int main() {
int arr[10] = {2, 5, 3, 7, 1, 8, 4, 9, 6, 10};
int n = sizeof(arr) / sizeof(arr[0]);
selection_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
运行结果:
```
Sorted array: 1 2 3 4 5 6 7 8 9 10
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)