用选择法对10个整数排序代码
时间: 2023-03-29 18:00:49 浏览: 81
以下是用选择法对10个整数排序的代码:
#include <stdio.h>
int main() {
int arr[10] = {5, 2, 9, 1, 7, 4, 6, 8, 3, };
int i, j, min, temp;
for (i = ; i < 9; i++) {
min = i;
for (j = i + 1; j < 10; j++) {
if (arr[j] < arr[min]) {
min = j;
}
}
if (min != i) {
temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
printf("排序后的数组:");
for (i = ; i < 10; i++) {
printf("%d ", arr[i]);
}
return ;
}
相关问题
用选择法对十个整数排序的c语言代码
以下是使用选择排序算法对十个整数进行排序的 C 语言代码:
```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[] = {9, 4, 2, 8, 7, 5, 1, 3, 6, 0};
int n = sizeof(arr) / sizeof(arr[0]);
int i;
printf("Original array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
selection_sort(arr, n);
printf("\nSorted array: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
输出结果:
```
Original array: 9 4 2 8 7 5 1 3 6 0
Sorted array: 0 1 2 3 4 5 6 7 8 9
```
希望这个代码能够帮到您,如果您有任何其他问题,请继续提问。
C语言用选择法对10个整数排序
以下是C语言用选择法对10个整数排序的代码实现:
```c
#include <stdio.h>
void sort(int b[10], int n) {
int i, j, k, m;
for (i = 0; i < n; i++) {
k = i; // 假设初始值为最大值
for (j = i + 1; j < n; j++) {
if (b[j] > b[k]) k = j; // 求出每一轮比较的最大值
}
if (k != i) {
m = b[i];
b[i] = b[k];
b[k] = m; // b[i]和b[k]数值互换,使得该轮比较后b[i]为最大值
}
}
}
int main() {
int i, a[10];
printf("请输入十个整数:\n");
for (i = 0; i < 10; i++) scanf("%d", &a[i]); // 用scanf输入数组
sort(a, 10); // 调用排序函数
printf("用选择法对十个整数从大到小排序结果为:\n");
for (i = 0; i < 10; i++) printf("%5d", a[i]);
printf("\n");
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)