用选择排序法对数组中的10个整数按由小到大排序的c语言代码
时间: 2024-05-12 07:14:59 浏览: 105
以下是使用选择排序法对数组中的10个整数按由小到大排序的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[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
}
int main() {
int arr[10] = {9, 4, 6, 2, 1, 5, 8, 3, 7, 0};
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: 0 1 2 3 4 5 6 7 8 9
```
阅读全文