用c语言写一个编程,用起泡法对10个数排序(由小到大)
时间: 2024-05-12 19:20:44 浏览: 20
```
#include <stdio.h>
int main()
{
int i, j, temp;
int arr[10] = {4, 6, 3, 1, 8, 7, 9, 2, 5, 10};
// 冒泡排序
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;
}
}
}
// 输出排序后的数组
for(i = 0; i < 10; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
相关问题
用c语言写一个程序用起泡法对输入的10个字符由小到大顺序排列
好的,下面是用C语言实现冒泡排序的程序:
```c
#include <stdio.h>
void bubble_sort(char arr[], int n) {
int i, j;
char 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() {
char arr[10];
int i;
printf("请输入10个字符:\n");
for (i = 0; i < 10; i++) {
scanf("%c", &arr[i]);
}
bubble_sort(arr, 10);
printf("排序后的结果为:\n");
for (i = 0; i < 10; i++) {
printf("%c ", arr[i]);
}
printf("\n");
return 0;
}
```
程序通过 `bubble_sort` 函数实现冒泡排序,主函数中先读入 10 个字符,然后调用 `bubble_sort` 函数对字符数组进行排序,最后输出排序后的结果。
写一个函数,使用c语言用起泡法对输入的十个字符由小到大排序
以下是使用冒泡排序法对10个字符进行由小到大排序的C语言代码:
```c
#include <stdio.h>
void bubbleSort(char arr[], int n) {
int i, j;
char 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() {
char arr[10];
int i;
printf("Enter 10 characters: ");
for (i = 0; i < 10; i++) {
scanf("%c", &arr[i]);
}
bubbleSort(arr, 10);
printf("Sorted characters: ");
for (i = 0; i < 10; i++) {
printf("%c ", arr[i]);
}
printf("\n");
return 0;
}
```
在上面的代码中,`bubbleSort` 函数采用冒泡排序算法对输入的字符数组进行排序。在 `main` 函数中,首先从用户输入读取10个字符,并将它们存储在字符数组 `arr` 中。然后,调用 `bubbleSort` 函数对这个数组进行排序,并将排序后的结果打印到控制台上。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)