用C语言实现用选择法对10个整数排序。10 个整数用 scan函数输人
时间: 2023-11-21 22:38:38 浏览: 55
我可以帮您解决这个问题。以下是用C语言实现用选择法对10个整数排序的代码:
#include <stdio.h>
int main() {
int nums[10];
int i, j, temp;
// 输入10个整数
for (i = 0; i < 10; i++) {
printf("请输入第%d个整数:", i+1);
scanf("%d", &nums[i]);
}
// 选择法排序
for (i = 0; i < 9; i++)
for (j = i+1; j < 10; j++)
if (nums[i] > nums[j]) {
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
// 输出排序结果
printf("排序后的结果是:\n");
for (i = 0; i < 10; i++)
printf("%d ", nums[i]);
printf("\n");
return 0;
}
相关问题
用c语言实现选择法对10个整数排序
好的,以下是用C语言实现选择法对10个整数排序的代码:
```c
#include <stdio.h>
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i; // 先假设最小值为当前位置
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j; // 找到更小的值,更新最小值的下标
}
}
// 将最小值与当前位置交换
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
int main() {
int arr[10] = {4, 2, 8, 6, 1, 9, 5, 3, 7, 0};
int i;
printf("排序前的数组为:");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
selectionSort(arr, 10);
printf("排序后的数组为:");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
这段代码定义了一个`selectionSort`函数来实现选择排序。该函数使用两层循环,每次找到当前位置之后的最小值,然后将最小值与当前位置交换。最后,在`main`函数中调用`selectionSort`函数对数组进行排序,并输出排序前后的数组。
C语言编写用选择法对10个整数排序
以下是使用选择法对10个整数进行排序的C语言代码:
```c
#include <stdio.h>
int main() {
int arr[10];
int i, j, min, temp;
printf("Please enter 10 numbers: ");
for (i = 0; i < 10; i++) {
scanf("%d", &arr[i]);
}
for (i = 0; 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("Sorted array: ");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)