用冒泡法对 10 个整数排序。
时间: 2023-05-11 14:05:03 浏览: 123
以下是用冒泡法对 10 个整数排序的代码:
```
#include <stdio.h>
void bubble_sort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; 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, 3, 1, 6, 9, 4, 7, 0};
int n = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
输出结果为:
```
Sorted array: 0 1 2 3 4 5 6 7 8 9
```
希望能够帮到您!
相关问题
用冒泡法对10个整数排序,要求10个整数用scanf函数输入
使用冒泡法将10个整数排序,要求输入10个整数用scanf函数输入。
冒泡法是一种简单的排序算法,在它的基本形式中,它重复地走访过要排序的数列,依次比较相邻的两个元素,如果它们的顺序错误就把它们交换过来。经过一轮的比较后,可以确保该轮最后一个数为最大值;接着进行下一轮比较,直到所有元素都排序完成。
具体实现方法如下:
1. 输入10个整数,可以使用scanf函数输入。
2. 使用嵌套循环进行冒泡排序,外层循环控制排序轮数,内层循环控制每轮比较的次数。
3. 在内层循环中,比较相邻的两个数的大小,如果前一个数比后一个数大就交换它们的位置。
4. 每完成一轮排序,最后一个数就是最大的数,因此下一轮排序的循环次数可以减少1。
最终得到的排序后的整数序列如下:
C语言编写用冒泡法对 10 个整数排序程序,并输出
下面是C语言冒泡排序的代码:
```c
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j for (i = 0; i < n1; i++) {
for (j = ; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// swap[j] and arr[j+1 int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[10] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
int i;
bubbleSort(arr, 10);
printf("Sorted array: ");
for (i=0; i < 10; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}
```
这个程序使用了冒泡排序算法对10个整数进行排序,并输出排序后的结果。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](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)