c语言中有数组int arr[10] = {6,3,5,2,4,20,7,1,23,11}; 数组倒序 实现 {11,23,1,7,20,4,2,5,3,6} 数组排序 实现{1,2,3,4,5,6,7,11,20,23}
时间: 2023-08-19 11:08:52 浏览: 74
实现数组的倒序可以通过交换数组元素的位置来实现。以下是一种实现方法:
```c
#include <stdio.h>
void reverseArray(int arr[], int length) {
int start = 0;
int end = length - 1;
while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
int main() {
int arr[10] = {6,3,5,2,4,20,7,1,23,11};
int length = sizeof(arr) / sizeof(arr[0]);
printf("原始数组:");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
reverseArray(arr, length);
printf("\n倒序数组:");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
实现数组的排序可以使用标准库函数 `qsort` 来实现。以下是一种实现方法:
```c
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b) {
return (*(int*)a - *(int*)b);
}
int main() {
int arr[10] = {6,3,5,2,4,20,7,1,23,11};
int length = sizeof(arr) / sizeof(arr[0]);
printf("原始数组:");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
qsort(arr, length, sizeof(int), compare);
printf("\n排序数组:");
for (int i = 0; i < length; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
以上代码中,我们使用 `qsort` 函数进行排序,并自定义了一个比较函数 `compare` 来指定排序规则。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.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)