用c语言用选择法对10个整数递减排序
时间: 2023-11-21 07:00:18 浏览: 40
当然可以。以下是一个简单的用选择排序算法对10个整数递减排序的 C 语言代码:
```
#include <stdio.h>
void selectionSort(int arr[], int n);
int main()
{
int arr[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
printf("Before sorting: ");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
selectionSort(arr, 10);
printf("After sorting: ");
for (int i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
void selectionSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++) {
int maxIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[maxIdx]) {
maxIdx = j;
}
}
int tmp = arr[i];
arr[i] = arr[maxIdx];
arr[maxIdx] = tmp;
}
}
```
运行结果:
```
Before sorting: 10 9 8 7 6 5 4 3 2 1
After sorting: 1 2 3 4 5 6 7 8 9 10
```
希望这个简单的例子能够帮助你明白选择排序的基本原理和实现。