用C语言写一个冒泡排序
时间: 2023-03-27 22:04:31 浏览: 43
可以的,以下是一个用 C 语言实现的冒泡排序的代码示例:
```c
#include <stdio.h>
void bubble_sort(int arr[], int len) {
int i, j, temp;
for (i = ; i < len - 1; i++) {
for (j = ; j < len - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {5, 2, 8, 4, 7, 1, 3, 9, 6};
int len = sizeof(arr) / sizeof(arr[]);
int i;
printf("排序前的数组:");
for (i = ; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
bubble_sort(arr, len);
printf("排序后的数组:");
for (i = ; i < len; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return ;
}
```
这个程序会输出排序前和排序后的数组,你可以自己运行一下看看效果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)