你好用C语言编写对主函数数组a降序排列
以下是使用C语言编写的对主函数数组a降序排列的代码:
#include <stdio.h>
void sort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main() {
int a[] = {5, 2, 8, 3, 1};
int n = sizeof(a) / sizeof(a[0]);
int i;
printf("Before sorting: ");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
sort(a, n);
printf("After sorting: ");
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
运行结果:
Before sorting: 5 2 8 3 1
After sorting: 8 5 3 2 1
``
原文地址: https://www.cveoy.top/t/topic/dx5l 著作权归作者所有。请勿转载和采集!