c语言分别用"冒泡法"对 10 个整数按由小到大排序。10 个整数为 35、68、 97、2、56、7、9、144、74、5。
时间: 2023-07-04 17:29:23 浏览: 37
好的,下面是使用C语言实现冒泡排序的代码,对于给定的10个整数进行由小到大排序:
```c
#include <stdio.h>
int main()
{
int arr[10] = {35, 68, 97, 2, 56, 7, 9, 144, 74, 5};
int i, j, temp;
// 冒泡排序
for (i = 0; i < 9; i++) {
for (j = 0; j < 9 - i; j++) {
if (arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
// 输出排序后的结果
printf("排序后的结果为:\n");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
输出结果为:
```
排序后的结果为:
2 5 7 9 35 56 68 74 97 144
```
希望这个例子可以帮助您理解冒泡排序的思想和实现方法。
相关问题
C语言 用冒泡排序法对10个整数由小到大排序
好的,下面是 C 语言代码实现冒泡排序法对10个整数由小到大排序:
```c
#include <stdio.h>
int main() {
int arr[10] = {6, 3, 8, 2, 9, 1, 5, 4, 7, 0};
int temp, i, j;
// 冒泡排序
for (i = 0; i < 10 - 1; i++) {
for (j = 0; j < 10 - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// 输出排序结果
printf("排序后的数组为:\n");
for (i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
代码中,我们定义了一个包含 10 个元素的整型数组 `arr`,并初始化了其中的元素。然后使用两层循环嵌套实现冒泡排序,最后输出排序后的结果。
希望能对您有所帮助!
c语言分别用“冒泡法”对 10 个整数按由小到大排序。10 个整数为 35、68、 97、2、56、7、9、144、74、5
好的,这里是使用 C 语言实现冒泡排序的代码:
```c
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 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[] = { 35, 68, 97, 2, 56, 7, 9, 144, 74, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("排序结果:");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
输出结果为:
```
排序结果:2 5 7 9 35 56 68 74 97 144
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)