给一百个数字排列大小c语言
时间: 2024-09-09 12:02:48 浏览: 51
在C语言中,给定一组乱序的100个整数,你可以使用数组或者其他数据结构(如链表或动态分配内存)来存储它们,并对它们进行排序。一种常见的排序算法是冒泡排序、选择排序、插入排序、快速排序或归并排序等。
这里以冒泡排序为例,它是一种简单的排序算法:
```c
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
// 交换元素
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
// 测试函数
int main() {
int arr[100]; // 假设数组长度是100
// 填充随机值
for (int i = 0; i < 100; i++) {
arr[i] = rand(); // 使用rand()生成随机数
}
// 排序
bubbleSort(arr, 100);
// 输出排序后的数组
for (int i = 0; i < 100; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)