(1)写一个函数,用“起泡法”对输人的10个字符按由小到大顺序排列(本题是第7章 第11题)。\n①输人程序,进行编译和运行,分析结果。\n②将要排序的字符串改为5个,按由大到小的顺序排列
时间: 2023-06-01 07:02:20 浏览: 278
### 回答1:
这题需要编写一个函数,使用“起泡法”对输入的10个字符按由小到大顺序排列(本题是第7章第11题)。
①输入程序,进行编译和运行,分析结果。
②将要排序的字符串改为5个字符,按由大到小顺序排列。
答案:
①实现代码如下:
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[10];
int i, j, temp;
printf("请输入10个字符:");
scanf("%s", str);
for (i = 0; i < strlen(str)-1; i++)
{
for (j = 1; j < strlen(str)-i; j++)
{
if (str[j-1] > str[j])
{
temp = str[j-1];
str[j-1] = str[j];
str[j] = temp;
}
}
}
printf("排序后的字符串为:%s\n", str);
return 0;
}
②修改排序的方式,代码如下:
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[5];
int i, j, temp;
printf("请输入5个字符:");
scanf("%s", str);
for (i = 0; i < strlen(str)-1; i++)
{
for (j = 1; j < strlen(str)-i; j++)
{
if (str[j-1] < str[j])
{
temp = str[j-1];
str[j-1] = str[j];
str[j] = temp;
}
}
}
printf("按由大到小顺序排列后的字符串为:%s\n", str);
return 0;
}
### 回答2:
1. 基于起泡法对10个字符进行按由小到大的排序,可以如下编写函数:
```c++
void bubbleSort(char arr[], int n){
for (int i = 0; i < n-1; i++){
for (int j = 0; j < n-i-1; j++){
if (arr[j] > arr[j+1]){
char temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
```
我们可以写一个测试函数来调用上面的函数,如下所示:
```c++
int main() {
char arr[10] = {'c', 'd', 'a', 'f', 'g', 'b', 'r', 'y', 's', 'e'};
bubbleSort(arr, 10);
for(int i = 0; i < 10; i++){
cout << arr[i] << " ";
}
return 0;
}
```
以上程序编译和运行结果如下:
```
a b c d e f g r s y
```
分析结果可以发现,输出结果符合我们的预期,按照由小到大的顺序进行了排序。
2. 如果我们将要排序的字符串改为5个,按由大到小的顺序排列,可以按以下方式修改代码,使其适用于新的字符串。
```c++
int main() {
char arr[5] = {'c', 'd', 'a', 'f', 'g'};
bubbleSort(arr, 5);
for(int i = 4; i >= 0; i--){
cout << arr[i] << " ";
}
return 0;
}
```
输出结果如下:
```
g f d c a
```
我们可以看到字符串已经按照由大到小的顺序进行了排序。这是因为我们将排序算法按照由小到大进行排序修改为由大到小排序,同时修改了输出数组元素的方式。
### 回答3:
1. 实现起泡法排序的函数如下:
```
void bubbleSort(char *str, int len) {
int i, j;
char temp;
for (i = 0; i < len - 1; i++) {
for (j = len - 1; j > i; j--) {
if (str[j] < str[j - 1]) {
temp = str[j];
str[j] = str[j - 1];
str[j - 1] = temp;
}
}
}
}
```
① 其中,str为要排序的字符串,len为字符串的长度。该函数采用双重循环实现起泡法排序,第一次内循环从最后一个字符开始遍历到当前外循环位置,将相邻的字符交换,使较小的字符逐步“冒泡”到前面。第二次内循环同理,直到所有字符都按照从小到大的顺序排列。
在编译和运行时,可以按照以下方式输入并调用该函数:
```
int main() {
char str[10];
printf("请输入10个字符:");
scanf("%s", str);
bubbleSort(str, 10);
printf("排序后的结果为:%s\n", str);
return 0;
}
```
当输入"edacbfhgij"时,程序输出的结果为"abcdefghij",符合从小到大的顺序排列。
② 如果要将要排序的字符串改为5个,按由大到小的顺序排列,只需要修改一下排序函数:
```
void bubbleSort(char *str, int len) {
int i, j;
char temp;
for (i = 0; i < len - 1; i++) {
for (j = len - 1; j > i; j--) {
if (str[j] > str[j - 1]) { //修改这一行判断大于号
temp = str[j];
str[j] = str[j - 1];
str[j - 1] = temp;
}
}
}
}
```
其他地方保持不变。当输入"edacb"时,程序输出的结果为"edcba",符合从大到小的顺序排列。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)